Skip to content

Commit 6ebeef4

Browse files
Merge pull request #3577 from nextcloud/701-fix
701-fix
2 parents b962e38 + 30535ff commit 6ebeef4

21 files changed

+288
-185
lines changed

File Provider Extension/FileProviderExtension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ class FileProviderExtension: NSFileProviderExtension {
220220
assert(pathComponents.count > 2)
221221
let itemIdentifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
222222
let fileName = pathComponents[pathComponents.count - 1]
223-
guard let metadata = self.database.getMetadataFromOcIdAndocIdTransfer(itemIdentifier.rawValue) else {
223+
guard let metadata = await self.database.getMetadataFromOcIdAndocIdTransferAsync(itemIdentifier.rawValue) else {
224224
return
225225
}
226226
let serverUrlFileName = metadata.serverUrl + "/" + fileName

Nextcloud.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5872,7 +5872,7 @@
58725872
CLANG_WARN_UNREACHABLE_CODE = YES;
58735873
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
58745874
COPY_PHASE_STRIP = NO;
5875-
CURRENT_PROJECT_VERSION = 2;
5875+
CURRENT_PROJECT_VERSION = 3;
58765876
DEBUG_INFORMATION_FORMAT = dwarf;
58775877
DEVELOPMENT_TEAM = NKUJUXUJ3B;
58785878
ENABLE_STRICT_OBJC_MSGSEND = YES;
@@ -5938,7 +5938,7 @@
59385938
CLANG_WARN_UNREACHABLE_CODE = YES;
59395939
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
59405940
COPY_PHASE_STRIP = NO;
5941-
CURRENT_PROJECT_VERSION = 2;
5941+
CURRENT_PROJECT_VERSION = 3;
59425942
DEVELOPMENT_TEAM = NKUJUXUJ3B;
59435943
ENABLE_STRICT_OBJC_MSGSEND = YES;
59445944
ENABLE_TESTABILITY = YES;

iOSClient/Data/NCManageDatabase+Account.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,21 @@ extension NCManageDatabase {
518518
} ?? NCBrandOptions.shared.folderDefaultAutoUpload
519519
}
520520

521+
func getAccountAutoUploadFileNameAsync(account: String) async -> String {
522+
let result: String? = await performRealmReadAsync { realm in
523+
guard let record = realm.objects(tableAccount.self)
524+
.filter("account == %@", account)
525+
.first
526+
else {
527+
return nil
528+
}
529+
530+
return record.autoUploadFileName.isEmpty ? nil : record.autoUploadFileName
531+
}
532+
533+
return result ?? NCBrandOptions.shared.folderDefaultAutoUpload
534+
}
535+
521536
func getAccountAutoUploadDirectory(session: NCSession.Session) -> String {
522537
return getAccountAutoUploadDirectory(account: session.account, urlBase: session.urlBase, userId: session.userId)
523538
}
@@ -535,17 +550,43 @@ extension NCManageDatabase {
535550
} ?? homeServer
536551
}
537552

553+
func getAccountAutoUploadDirectoryAsync(account: String, urlBase: String, userId: String) async -> String {
554+
let homeServer = utilityFileSystem.getHomeServer(urlBase: urlBase, userId: userId)
555+
556+
let directory: String? = await performRealmReadAsync { realm in
557+
realm.objects(tableAccount.self)
558+
.filter("account == %@", account)
559+
.first?
560+
.autoUploadDirectory
561+
}
562+
563+
return directory.flatMap { dir in
564+
(dir.isEmpty || dir.contains("/webdav")) ? homeServer : dir
565+
} ?? homeServer
566+
}
567+
538568
func getAccountAutoUploadServerUrlBase(session: NCSession.Session) -> String {
539569
return getAccountAutoUploadServerUrlBase(account: session.account, urlBase: session.urlBase, userId: session.userId)
540570
}
541571

572+
func getAccountAutoUploadServerUrlBaseAsync(session: NCSession.Session) async -> String {
573+
return await getAccountAutoUploadServerUrlBaseAsync(account: session.account, urlBase: session.urlBase, userId: session.userId)
574+
}
575+
542576
func getAccountAutoUploadServerUrlBase(account: String, urlBase: String, userId: String) -> String {
543577
let cameraFileName = self.getAccountAutoUploadFileName(account: account)
544578
let cameraDirectory = self.getAccountAutoUploadDirectory(account: account, urlBase: urlBase, userId: userId)
545579
let folderPhotos = utilityFileSystem.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)
546580
return folderPhotos
547581
}
548582

583+
func getAccountAutoUploadServerUrlBaseAsync(account: String, urlBase: String, userId: String) async -> String {
584+
let cameraFileName = await self.getAccountAutoUploadFileNameAsync(account: account)
585+
let cameraDirectory = await self.getAccountAutoUploadDirectoryAsync(account: account, urlBase: urlBase, userId: userId)
586+
let folderPhotos = utilityFileSystem.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)
587+
return folderPhotos
588+
}
589+
549590
func getAccountAutoUploadSubfolderGranularity() -> Int {
550591
performRealmRead { realm in
551592
realm.objects(tableAccount.self)

iOSClient/Data/NCManageDatabase+AutoUpload.swift

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ extension NCManageDatabase {
5757

5858
// MARK: - Realm Read
5959

60-
func fetchSkipFileNames(account: String, autoUploadServerUrlBase: String) async -> Set<String> {
60+
func fetchSkipFileNames(account: String,
61+
autoUploadServerUrlBase: String) async -> Set<String> {
6162
let result: Set<String>? = await performRealmReadAsync { realm in
6263
let metadatas = realm.objects(tableMetadata.self)
6364
.filter("account == %@ AND autoUploadServerUrlBase == %@ AND status IN %@", account, autoUploadServerUrlBase, NCGlobal.shared.metadataStatusUploadingAllMode)
@@ -78,7 +79,8 @@ extension NCManageDatabase {
7879
/// - account: The account identifier.
7980
/// - autoUploadServerUrlBase: The server base URL for auto-upload.
8081
/// - Returns: The most recent upload `Date`, or `nil` if no entry exists.
81-
func fetchLastAutoUploadedDateAsync(account: String, autoUploadServerUrlBase: String) async -> Date? {
82+
func fetchLastAutoUploadedDateAsync(account: String,
83+
autoUploadServerUrlBase: String) async -> Date? {
8284
await performRealmReadAsync { realm in
8385
realm.objects(tableAutoUploadTransfer.self)
8486
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
@@ -87,11 +89,21 @@ extension NCManageDatabase {
8789
}
8890
}
8991

90-
func existsAutoUpload(account: String, autoUploadServerUrlBase: String) -> Bool {
92+
func existsAutoUpload(account: String,
93+
autoUploadServerUrlBase: String) -> Bool {
9194
return performRealmRead { realm in
9295
realm.objects(tableAutoUploadTransfer.self)
9396
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
9497
.first != nil
9598
} ?? false
9699
}
100+
101+
func existsAutoUploadAsync(account: String,
102+
autoUploadServerUrlBase: String) async -> Bool {
103+
return await performRealmReadAsync { realm in
104+
realm.objects(tableAutoUploadTransfer.self)
105+
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
106+
.first != nil
107+
} ?? false
108+
}
97109
}

iOSClient/Data/NCManageDatabase+Metadata.swift

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -471,14 +471,6 @@ extension NCManageDatabase {
471471
}
472472
}
473473

474-
func deleteMetadatas(_ metadatas: [tableMetadata], sync: Bool = true) {
475-
let detached = metadatas.map { $0.detachedCopy() }
476-
477-
performRealmWrite(sync: sync) { realm in
478-
realm.delete(detached)
479-
}
480-
}
481-
482474
// Asynchronously deletes an array of `tableMetadata` entries from the Realm database.
483475
/// - Parameter metadatas: The `tableMetadata` objects to be deleted.
484476
func deleteMetadatasAsync(_ metadatas: [tableMetadata]) async {
@@ -1017,16 +1009,27 @@ extension NCManageDatabase {
10171009

10181010
func getMetadatasAsync(predicate: NSPredicate,
10191011
sortedByKeyPath: String,
1020-
ascending: Bool = false) async -> [tableMetadata]? {
1012+
ascending: Bool = false,
1013+
limit: Int? = nil) async -> [tableMetadata]? {
10211014
return await performRealmReadAsync { realm in
1022-
realm.objects(tableMetadata.self)
1015+
let results = realm.objects(tableMetadata.self)
10231016
.filter(predicate)
1024-
.sorted(byKeyPath: sortedByKeyPath, ascending: ascending)
1025-
.map { $0.detachedCopy() }
1017+
.sorted(byKeyPath: sortedByKeyPath,
1018+
ascending: ascending)
1019+
1020+
if let limit {
1021+
let sliced = results.prefix(limit)
1022+
return sliced.map { $0.detachedCopy() }
1023+
} else {
1024+
return results.map { $0.detachedCopy() }
1025+
}
10261026
}
10271027
}
10281028

1029-
func getMetadatas(predicate: NSPredicate, numItems: Int, sorted: String, ascending: Bool) -> [tableMetadata] {
1029+
func getMetadatas(predicate: NSPredicate,
1030+
numItems: Int,
1031+
sorted: String,
1032+
ascending: Bool) -> [tableMetadata] {
10301033
return performRealmRead { realm in
10311034
let results = realm.objects(tableMetadata.self)
10321035
.filter(predicate)
@@ -1058,17 +1061,6 @@ extension NCManageDatabase {
10581061
}
10591062
}
10601063

1061-
func getMetadataFromOcIdAndocIdTransfer(_ ocId: String?) -> tableMetadata? {
1062-
guard let ocId else { return nil }
1063-
1064-
return performRealmRead { realm in
1065-
realm.objects(tableMetadata.self)
1066-
.filter("ocId == %@ OR ocIdTransfer == %@", ocId, ocId)
1067-
.first
1068-
.map { $0.detachedCopy() }
1069-
}
1070-
}
1071-
10721064
func getMetadataFromOcIdAndocIdTransferAsync(_ ocId: String?) async -> tableMetadata? {
10731065
guard let ocId else {
10741066
return nil
@@ -1215,13 +1207,16 @@ extension NCManageDatabase {
12151207
} ?? []
12161208
}
12171209

1218-
func getMetadatas(predicate: NSPredicate, sortedByKeyPath: String, ascending: Bool, arraySlice: Int) -> [tableMetadata] {
1210+
func getMetadatas(predicate: NSPredicate,
1211+
sortedByKeyPath: String,
1212+
ascending: Bool,
1213+
limit: Int) -> [tableMetadata] {
12191214
return performRealmRead { realm in
12201215
let results = realm.objects(tableMetadata.self)
12211216
.filter(predicate)
12221217
.sorted(byKeyPath: sortedByKeyPath, ascending: ascending)
12231218
.map { $0.detachedCopy() }
1224-
.prefix(arraySlice)
1219+
.prefix(limit)
12251220
return Array(results)
12261221
} ?? []
12271222
}

iOSClient/Data/NCManageDatabase+SecurityGuard.swift

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,6 @@ extension NCManageDatabase {
3232

3333
// MARK: - Realm write
3434

35-
func addDiagnostic(account: String, issue: String, error: String? = nil, sync: Bool = true) {
36-
performRealmWrite(sync: sync) { realm in
37-
let primaryKey = account + issue + (error ?? "")
38-
39-
if let result = realm.object(ofType: TableSecurityGuardDiagnostics.self, forPrimaryKey: primaryKey) {
40-
result.counter += 1
41-
result.oldest = Date().timeIntervalSince1970
42-
} else {
43-
let table = TableSecurityGuardDiagnostics(account: account, issue: issue, error: error, date: Date())
44-
realm.add(table)
45-
}
46-
}
47-
}
48-
4935
func addDiagnosticAsync(account: String,
5036
issue: String,
5137
error: String? = nil) async {
@@ -78,23 +64,13 @@ extension NCManageDatabase {
7864

7965
// MARK: - Realm read
8066

81-
func existsDiagnostics(account: String) -> Bool {
82-
var exists = false
83-
performRealmRead { realm in
67+
func existsDiagnosticsAsync(account: String) async -> Bool {
68+
let exists: Bool? = await performRealmReadAsync { realm in
8469
let results = realm.objects(TableSecurityGuardDiagnostics.self)
8570
.filter("account == %@", account)
86-
exists = !results.isEmpty
87-
}
88-
return exists
89-
}
90-
91-
func getDiagnostics(account: String, issue: String) -> Results<TableSecurityGuardDiagnostics>? {
92-
var results: Results<TableSecurityGuardDiagnostics>?
93-
performRealmRead { realm in
94-
results = realm.objects(TableSecurityGuardDiagnostics.self)
95-
.filter("account == %@ AND issue == %@", account, issue)
71+
return !results.isEmpty
9672
}
97-
return results
73+
return exists ?? false
9874
}
9975

10076
func getDiagnosticsAsync(account: String) async -> [TableSecurityGuardDiagnostics]? {

iOSClient/Data/NCManageDatabase.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ final class NCManageDatabase: @unchecked Sendable {
9696
let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
9797
let databaseFileUrl = dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName)
9898

99+
// now you can read/write in Realm
100+
isAppSuspending = false
101+
99102
Realm.Configuration.defaultConfiguration = Realm.Configuration(fileURL: databaseFileUrl,
100103
schemaVersion: databaseSchemaVersion,
101104
migrationBlock: { migration, oldSchemaVersion in
@@ -107,12 +110,11 @@ final class NCManageDatabase: @unchecked Sendable {
107110
if let url = realm.configuration.fileURL {
108111
nkLog(start: "Realm is located at: \(url.path)")
109112
}
113+
return true
110114
} catch {
111115
nkLog(error: "Realm error: \(error)")
112116
return false
113117
}
114-
115-
return true
116118
}
117119

118120
private func openRealmAppex() {
@@ -200,6 +202,11 @@ final class NCManageDatabase: @unchecked Sendable {
200202

201203
@discardableResult
202204
func performRealmRead<T>(_ block: @escaping (Realm) throws -> T?, sync: Bool = true, completion: ((T?) -> Void)? = nil) -> T? {
205+
// Skip execution if app is suspending
206+
guard !isAppSuspending else {
207+
completion?(nil)
208+
return nil
209+
}
203210
let isOnRealmQueue = DispatchQueue.getSpecific(key: NCManageDatabase.realmQueueKey) != nil
204211

205212
if sync {
@@ -241,6 +248,11 @@ final class NCManageDatabase: @unchecked Sendable {
241248
}
242249

243250
func performRealmWrite(sync: Bool = true, _ block: @escaping (Realm) throws -> Void) {
251+
// Skip execution if app is suspending
252+
guard !isAppSuspending
253+
else {
254+
return
255+
}
244256
let isOnRealmQueue = DispatchQueue.getSpecific(key: NCManageDatabase.realmQueueKey) != nil
245257

246258
let executionBlock: @Sendable () -> Void = {
@@ -271,7 +283,12 @@ final class NCManageDatabase: @unchecked Sendable {
271283
// MARK: - performRealmRead async/await, performRealmWrite async/await
272284

273285
func performRealmReadAsync<T>(_ block: @escaping (Realm) throws -> T?) async -> T? {
274-
await withCheckedContinuation { continuation in
286+
// Skip execution if app is suspending
287+
guard !isAppSuspending else {
288+
return nil
289+
}
290+
291+
return await withCheckedContinuation { continuation in
275292
realmQueue.async {
276293
autoreleasepool {
277294
do {
@@ -288,6 +305,11 @@ final class NCManageDatabase: @unchecked Sendable {
288305
}
289306

290307
func performRealmWriteAsync(_ block: @escaping (Realm) throws -> Void) async {
308+
// Skip execution if app is suspending
309+
if isAppSuspending {
310+
return
311+
}
312+
291313
await withCheckedContinuation { continuation in
292314
realmQueue.async {
293315
autoreleasepool {

iOSClient/Main/Collection Common/NCCollectionViewCommon.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,10 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
244244
let dropInteraction = UIDropInteraction(delegate: self)
245245
self.navigationController?.navigationItem.leftBarButtonItems?.first?.customView?.addInteraction(dropInteraction)
246246

247-
NotificationCenter.default.addObserver(self, selector: #selector(changeTheming(_:)), name: NSNotification.Name(rawValue: global.notificationCenterChangeTheming), object: nil)
247+
NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: self.global.notificationCenterChangeTheming), object: nil, queue: .main) { [weak self] _ in
248+
guard let self else { return }
249+
self.collectionView.reloadData()
250+
}
248251

249252
DispatchQueue.main.async {
250253
self.collectionView?.collectionViewLayout.invalidateLayout()
@@ -528,12 +531,6 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
528531
self.resetPlusButtonAlpha()
529532
}
530533

531-
@objc func changeTheming(_ notification: NSNotification) {
532-
Task {
533-
await self.reloadDataSource()
534-
}
535-
}
536-
537534
@objc func closeRichWorkspaceWebView() {
538535
Task {
539536
await self.reloadDataSource()

0 commit comments

Comments
 (0)