Skip to content
Merged

701-fix #3577

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion File Provider Extension/FileProviderExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ class FileProviderExtension: NSFileProviderExtension {
assert(pathComponents.count > 2)
let itemIdentifier = NSFileProviderItemIdentifier(pathComponents[pathComponents.count - 2])
let fileName = pathComponents[pathComponents.count - 1]
guard let metadata = self.database.getMetadataFromOcIdAndocIdTransfer(itemIdentifier.rawValue) else {
guard let metadata = await self.database.getMetadataFromOcIdAndocIdTransferAsync(itemIdentifier.rawValue) else {
return
}
let serverUrlFileName = metadata.serverUrl + "/" + fileName
Expand Down
4 changes: 2 additions & 2 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5872,7 +5872,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -5938,7 +5938,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 2;
CURRENT_PROJECT_VERSION = 3;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down
41 changes: 41 additions & 0 deletions iOSClient/Data/NCManageDatabase+Account.swift
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ extension NCManageDatabase {
} ?? NCBrandOptions.shared.folderDefaultAutoUpload
}

func getAccountAutoUploadFileNameAsync(account: String) async -> String {
let result: String? = await performRealmReadAsync { realm in
guard let record = realm.objects(tableAccount.self)
.filter("account == %@", account)
.first
else {
return nil
}

return record.autoUploadFileName.isEmpty ? nil : record.autoUploadFileName
}

return result ?? NCBrandOptions.shared.folderDefaultAutoUpload
}

func getAccountAutoUploadDirectory(session: NCSession.Session) -> String {
return getAccountAutoUploadDirectory(account: session.account, urlBase: session.urlBase, userId: session.userId)
}
Expand All @@ -535,17 +550,43 @@ extension NCManageDatabase {
} ?? homeServer
}

func getAccountAutoUploadDirectoryAsync(account: String, urlBase: String, userId: String) async -> String {
let homeServer = utilityFileSystem.getHomeServer(urlBase: urlBase, userId: userId)

let directory: String? = await performRealmReadAsync { realm in
realm.objects(tableAccount.self)
.filter("account == %@", account)
.first?
.autoUploadDirectory
}

return directory.flatMap { dir in
(dir.isEmpty || dir.contains("/webdav")) ? homeServer : dir
} ?? homeServer
}

func getAccountAutoUploadServerUrlBase(session: NCSession.Session) -> String {
return getAccountAutoUploadServerUrlBase(account: session.account, urlBase: session.urlBase, userId: session.userId)
}

func getAccountAutoUploadServerUrlBaseAsync(session: NCSession.Session) async -> String {
return await getAccountAutoUploadServerUrlBaseAsync(account: session.account, urlBase: session.urlBase, userId: session.userId)
}

func getAccountAutoUploadServerUrlBase(account: String, urlBase: String, userId: String) -> String {
let cameraFileName = self.getAccountAutoUploadFileName(account: account)
let cameraDirectory = self.getAccountAutoUploadDirectory(account: account, urlBase: urlBase, userId: userId)
let folderPhotos = utilityFileSystem.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)
return folderPhotos
}

func getAccountAutoUploadServerUrlBaseAsync(account: String, urlBase: String, userId: String) async -> String {
let cameraFileName = await self.getAccountAutoUploadFileNameAsync(account: account)
let cameraDirectory = await self.getAccountAutoUploadDirectoryAsync(account: account, urlBase: urlBase, userId: userId)
let folderPhotos = utilityFileSystem.stringAppendServerUrl(cameraDirectory, addFileName: cameraFileName)
return folderPhotos
}

func getAccountAutoUploadSubfolderGranularity() -> Int {
performRealmRead { realm in
realm.objects(tableAccount.self)
Expand Down
18 changes: 15 additions & 3 deletions iOSClient/Data/NCManageDatabase+AutoUpload.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ extension NCManageDatabase {

// MARK: - Realm Read

func fetchSkipFileNames(account: String, autoUploadServerUrlBase: String) async -> Set<String> {
func fetchSkipFileNames(account: String,
autoUploadServerUrlBase: String) async -> Set<String> {
let result: Set<String>? = await performRealmReadAsync { realm in
let metadatas = realm.objects(tableMetadata.self)
.filter("account == %@ AND autoUploadServerUrlBase == %@ AND status IN %@", account, autoUploadServerUrlBase, NCGlobal.shared.metadataStatusUploadingAllMode)
Expand All @@ -78,7 +79,8 @@ extension NCManageDatabase {
/// - account: The account identifier.
/// - autoUploadServerUrlBase: The server base URL for auto-upload.
/// - Returns: The most recent upload `Date`, or `nil` if no entry exists.
func fetchLastAutoUploadedDateAsync(account: String, autoUploadServerUrlBase: String) async -> Date? {
func fetchLastAutoUploadedDateAsync(account: String,
autoUploadServerUrlBase: String) async -> Date? {
await performRealmReadAsync { realm in
realm.objects(tableAutoUploadTransfer.self)
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
Expand All @@ -87,11 +89,21 @@ extension NCManageDatabase {
}
}

func existsAutoUpload(account: String, autoUploadServerUrlBase: String) -> Bool {
func existsAutoUpload(account: String,
autoUploadServerUrlBase: String) -> Bool {
return performRealmRead { realm in
realm.objects(tableAutoUploadTransfer.self)
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
.first != nil
} ?? false
}

func existsAutoUploadAsync(account: String,
autoUploadServerUrlBase: String) async -> Bool {
return await performRealmReadAsync { realm in
realm.objects(tableAutoUploadTransfer.self)
.filter("account == %@ AND serverUrlBase == %@", account, autoUploadServerUrlBase)
.first != nil
} ?? false
}
}
47 changes: 21 additions & 26 deletions iOSClient/Data/NCManageDatabase+Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,6 @@ extension NCManageDatabase {
}
}

func deleteMetadatas(_ metadatas: [tableMetadata], sync: Bool = true) {
let detached = metadatas.map { $0.detachedCopy() }

performRealmWrite(sync: sync) { realm in
realm.delete(detached)
}
}

// Asynchronously deletes an array of `tableMetadata` entries from the Realm database.
/// - Parameter metadatas: The `tableMetadata` objects to be deleted.
func deleteMetadatasAsync(_ metadatas: [tableMetadata]) async {
Expand Down Expand Up @@ -1017,16 +1009,27 @@ extension NCManageDatabase {

func getMetadatasAsync(predicate: NSPredicate,
sortedByKeyPath: String,
ascending: Bool = false) async -> [tableMetadata]? {
ascending: Bool = false,
limit: Int? = nil) async -> [tableMetadata]? {
return await performRealmReadAsync { realm in
realm.objects(tableMetadata.self)
let results = realm.objects(tableMetadata.self)
.filter(predicate)
.sorted(byKeyPath: sortedByKeyPath, ascending: ascending)
.map { $0.detachedCopy() }
.sorted(byKeyPath: sortedByKeyPath,
ascending: ascending)

if let limit {
let sliced = results.prefix(limit)
return sliced.map { $0.detachedCopy() }
} else {
return results.map { $0.detachedCopy() }
}
}
}

func getMetadatas(predicate: NSPredicate, numItems: Int, sorted: String, ascending: Bool) -> [tableMetadata] {
func getMetadatas(predicate: NSPredicate,
numItems: Int,
sorted: String,
ascending: Bool) -> [tableMetadata] {
return performRealmRead { realm in
let results = realm.objects(tableMetadata.self)
.filter(predicate)
Expand Down Expand Up @@ -1058,17 +1061,6 @@ extension NCManageDatabase {
}
}

func getMetadataFromOcIdAndocIdTransfer(_ ocId: String?) -> tableMetadata? {
guard let ocId else { return nil }

return performRealmRead { realm in
realm.objects(tableMetadata.self)
.filter("ocId == %@ OR ocIdTransfer == %@", ocId, ocId)
.first
.map { $0.detachedCopy() }
}
}

func getMetadataFromOcIdAndocIdTransferAsync(_ ocId: String?) async -> tableMetadata? {
guard let ocId else {
return nil
Expand Down Expand Up @@ -1215,13 +1207,16 @@ extension NCManageDatabase {
} ?? []
}

func getMetadatas(predicate: NSPredicate, sortedByKeyPath: String, ascending: Bool, arraySlice: Int) -> [tableMetadata] {
func getMetadatas(predicate: NSPredicate,
sortedByKeyPath: String,
ascending: Bool,
limit: Int) -> [tableMetadata] {
return performRealmRead { realm in
let results = realm.objects(tableMetadata.self)
.filter(predicate)
.sorted(byKeyPath: sortedByKeyPath, ascending: ascending)
.map { $0.detachedCopy() }
.prefix(arraySlice)
.prefix(limit)
return Array(results)
} ?? []
}
Expand Down
32 changes: 4 additions & 28 deletions iOSClient/Data/NCManageDatabase+SecurityGuard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,6 @@ extension NCManageDatabase {

// MARK: - Realm write

func addDiagnostic(account: String, issue: String, error: String? = nil, sync: Bool = true) {
performRealmWrite(sync: sync) { realm in
let primaryKey = account + issue + (error ?? "")

if let result = realm.object(ofType: TableSecurityGuardDiagnostics.self, forPrimaryKey: primaryKey) {
result.counter += 1
result.oldest = Date().timeIntervalSince1970
} else {
let table = TableSecurityGuardDiagnostics(account: account, issue: issue, error: error, date: Date())
realm.add(table)
}
}
}

func addDiagnosticAsync(account: String,
issue: String,
error: String? = nil) async {
Expand Down Expand Up @@ -78,23 +64,13 @@ extension NCManageDatabase {

// MARK: - Realm read

func existsDiagnostics(account: String) -> Bool {
var exists = false
performRealmRead { realm in
func existsDiagnosticsAsync(account: String) async -> Bool {
let exists: Bool? = await performRealmReadAsync { realm in
let results = realm.objects(TableSecurityGuardDiagnostics.self)
.filter("account == %@", account)
exists = !results.isEmpty
}
return exists
}

func getDiagnostics(account: String, issue: String) -> Results<TableSecurityGuardDiagnostics>? {
var results: Results<TableSecurityGuardDiagnostics>?
performRealmRead { realm in
results = realm.objects(TableSecurityGuardDiagnostics.self)
.filter("account == %@ AND issue == %@", account, issue)
return !results.isEmpty
}
return results
return exists ?? false
}

func getDiagnosticsAsync(account: String) async -> [TableSecurityGuardDiagnostics]? {
Expand Down
28 changes: 25 additions & 3 deletions iOSClient/Data/NCManageDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ final class NCManageDatabase: @unchecked Sendable {
let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
let databaseFileUrl = dirGroup?.appendingPathComponent(NCGlobal.shared.appDatabaseNextcloud + "/" + databaseName)

// now you can read/write in Realm
isAppSuspending = false

Realm.Configuration.defaultConfiguration = Realm.Configuration(fileURL: databaseFileUrl,
schemaVersion: databaseSchemaVersion,
migrationBlock: { migration, oldSchemaVersion in
Expand All @@ -107,12 +110,11 @@ final class NCManageDatabase: @unchecked Sendable {
if let url = realm.configuration.fileURL {
nkLog(start: "Realm is located at: \(url.path)")
}
return true
} catch {
nkLog(error: "Realm error: \(error)")
return false
}

return true
}

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

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

if sync {
Expand Down Expand Up @@ -241,6 +248,11 @@ final class NCManageDatabase: @unchecked Sendable {
}

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

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

func performRealmReadAsync<T>(_ block: @escaping (Realm) throws -> T?) async -> T? {
await withCheckedContinuation { continuation in
// Skip execution if app is suspending
guard !isAppSuspending else {
return nil
}

return await withCheckedContinuation { continuation in
realmQueue.async {
autoreleasepool {
do {
Expand All @@ -288,6 +305,11 @@ final class NCManageDatabase: @unchecked Sendable {
}

func performRealmWriteAsync(_ block: @escaping (Realm) throws -> Void) async {
// Skip execution if app is suspending
if isAppSuspending {
return
}

await withCheckedContinuation { continuation in
realmQueue.async {
autoreleasepool {
Expand Down
11 changes: 4 additions & 7 deletions iOSClient/Main/Collection Common/NCCollectionViewCommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,10 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
let dropInteraction = UIDropInteraction(delegate: self)
self.navigationController?.navigationItem.leftBarButtonItems?.first?.customView?.addInteraction(dropInteraction)

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

DispatchQueue.main.async {
self.collectionView?.collectionViewLayout.invalidateLayout()
Expand Down Expand Up @@ -528,12 +531,6 @@ class NCCollectionViewCommon: UIViewController, UIGestureRecognizerDelegate, UIS
self.resetPlusButtonAlpha()
}

@objc func changeTheming(_ notification: NSNotification) {
Task {
await self.reloadDataSource()
}
}

@objc func closeRichWorkspaceWebView() {
Task {
await self.reloadDataSource()
Expand Down
Loading
Loading