瀏覽代碼

added observeTableMetadata

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 年之前
父節點
當前提交
1015d0dcf0
共有 2 個文件被更改,包括 35 次插入31 次删除
  1. 7 7
      iOSClient/AppDelegate.swift
  2. 28 24
      iOSClient/Networking/NCNetworkingProcessUpload.swift

+ 7 - 7
iOSClient/AppDelegate.swift

@@ -189,6 +189,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         NKCommon.shared.writeLog("[INFO] Application did become active")
 
+        // START OBSERVE UPLOAD PROCESS
+        NCNetworkingProcessUpload.shared.observeTableMetadata()
+
         self.deletePasswordSession = false
 
         if !NCAskAuthorization.shared.isRequesting {
@@ -206,9 +209,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
             NKCommon.shared.writeLog("[INFO] Initialize Auto upload with \(items) uploads")
         }
 
-        // START UPLOAD PROCESS
-        NCNetworkingProcessUpload.shared.startTimer()
-
         NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterApplicationDidBecomeActive)
     }
 
@@ -243,14 +243,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         NKCommon.shared.writeLog("[INFO] Application will resign active")
 
+        // STOP OBSERVE UPLOAD PROCESS
+        NCNetworkingProcessUpload.shared.invalidateObserveTableMetadata()
+
         if CCUtility.getPrivacyScreenEnabled() {
             // Privacy
             showPrivacyProtectionWindow()
         }
 
-        // STOP UPLOAD PROCESS
-        NCNetworkingProcessUpload.shared.stopTimer()
-
         // Reload Widget
         WidgetCenter.shared.reloadAllTimelines()
 
@@ -371,7 +371,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
 
         NCAutoUpload.shared.initAutoUpload(viewController: nil) { items in
             NKCommon.shared.writeLog("[INFO] Refresh task auto upload with \(items) uploads")
-            NCNetworkingProcessUpload.shared.process { items in
+            NCNetworkingProcessUpload.shared.start { items in
                 NKCommon.shared.writeLog("[INFO] Refresh task upload process with \(items) uploads")
                 task.setTaskCompleted(success: true)
             }

+ 28 - 24
iOSClient/Networking/NCNetworkingProcessUpload.swift

@@ -25,6 +25,7 @@ import UIKit
 import NextcloudKit
 import Photos
 import JGProgressHUD
+import RealmSwift
 
 class NCNetworkingProcessUpload: NSObject {
     public static let shared: NCNetworkingProcessUpload = {
@@ -32,30 +33,36 @@ class NCNetworkingProcessUpload: NSObject {
         return instance
     }()
 
-    var timerProcess: Timer?
-
-    func startTimer() {
-        DispatchQueue.main.async {
-            self.timerProcess?.invalidate()
-            self.timerProcess = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.processTimer), userInfo: nil, repeats: true)
-        }
-    }
-
-    func stopTimer() {
-        DispatchQueue.main.async {
-            self.timerProcess?.invalidate()
+    private var notificationToken: NotificationToken?
+
+    func observeTableMetadata() {
+        let realm = try! Realm()
+        let results = realm.objects(tableMetadata.self)
+        notificationToken = results.observe { [weak self] (changes: RealmCollectionChange) in
+            switch changes {
+            case .initial:
+                print("Initial")
+            case .update(_, _, let insertions, let modifications):
+                if (insertions.count > 0 || modifications.count > 0) {
+                    self?.invalidateObserveTableMetadata()
+                    self?.start(completition: { items in
+                        print("[LOG] PROCESS-UPLOAD \(items)")
+                        DispatchQueue.main.async {  self?.observeTableMetadata() }
+                    })
+                }
+            case .error(let error):
+                NKCommon.shared.writeLog("[ERROR] Could not write to TableMetadata: \(error)")
+            }
         }
     }
 
-    @objc func processTimer() {
-        process { _ in }
+    func invalidateObserveTableMetadata() {
+        notificationToken?.invalidate()
     }
 
-    func process(completition: @escaping (_ items: Int) -> Void) {
+    func start(completition: @escaping (_ items: Int) -> Void) {
 
-        guard let account = NCManageDatabase.shared.getActiveAccount() else { return }
-
-        stopTimer()
+        guard let account = NCManageDatabase.shared.getActiveAccount() else { return completition(0) }
 
         let appDelegate = UIApplication.shared.delegate as! AppDelegate
         let applicationState = UIApplication.shared.applicationState
@@ -74,8 +81,6 @@ class NCNetworkingProcessUpload: NSObject {
 
             counterUpload = metadatasUpload.count
 
-            print("[LOG] PROCESS-UPLOAD \(counterUpload)")
-
             // Update Badge
             let counterBadge = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d OR status == %d", NCGlobal.shared.metadataStatusWaitUpload, NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading))
             NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUpdateBadgeNumber, userInfo: ["counter":counterBadge.count])
@@ -156,12 +161,11 @@ class NCNetworkingProcessUpload: NSObject {
                 // verify delete Asset Local Identifiers in auto upload (DELETE Photos album)
                 if applicationState == .active && counterUpload == 0 && !isPasscodePresented {
                     self.deleteAssetLocalIdentifiers(account: account.account) {
-                        self.startTimer()
+                        completition(counterUpload)
                     }
-                } else if applicationState == .active {
-                    self.startTimer()
+                } else {
+                    completition(counterUpload)
                 }
-                completition(counterUpload)
             })
         }
     }