marinofaggiana 4 years ago
parent
commit
99d7d52971
2 changed files with 21 additions and 19 deletions
  1. 10 14
      iOSClient/AppDelegate.m
  2. 11 5
      iOSClient/Networking/NCAutoUpload.swift

+ 10 - 14
iOSClient/AppDelegate.m

@@ -204,7 +204,7 @@
     [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
     
     // Initialize Auto upload
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{ }];
     
     // Read active directory
     [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterReloadDataSourceNetworkForced object:nil];
@@ -280,7 +280,7 @@
     [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:NCBrandGlobal.shared.notificationCenterMenuDetailClose object:nil];
     
     // Start Auto Upload
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{ }];
     
     // Start services
     [[NCService shared] startRequestServicesServer];
@@ -548,16 +548,17 @@
 -(void)handleBackgroundTask:(BGTask *)task API_AVAILABLE(ios(13.0))
 {
     if (self.account == nil || self.account.length == 0) {
+        [task setTaskCompletedWithSuccess:true];
         return;
     }
     
-    //do things with task
     [[NCCommunicationCommon shared] writeLog:@"Start handler background task"];
     
     // Verify new photo
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-    
-    //[task setTaskCompletedWithSuccess:true];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{
+        [[NCCommunicationCommon shared] writeLog:@"Completition handler background task"];
+        [task setTaskCompletedWithSuccess:true];
+    }];
 }
 
 #pragma mark Fetch
@@ -572,15 +573,10 @@
     [[NCCommunicationCommon shared] writeLog:@"Start perform Fetch"];
     
     // Verify new photo
-    [[NCAutoUpload shared] initAutoUploadWithViewController:nil];
-    
-    // after 20 sec
-    /*
-    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
-        [[NCCommunicationCommon shared] writeLog:@"End 20 sec. perform Fetch"];
+    [[NCAutoUpload shared] initAutoUploadWithViewController:nil completion:^{
+        [[NCCommunicationCommon shared] writeLog:@"Completition perform Fetch"];
         completionHandler(UIBackgroundFetchResultNoData);
-    });
-    */
+    }];
 }
 
 #pragma mark Operation Networking & Session

+ 11 - 5
iOSClient/Networking/NCAutoUpload.swift

@@ -68,7 +68,7 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
             if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
                 NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: nil) { (hasPermission) in
                     if hasPermission {
-                        self.uploadAssetsNewAndFull(viewController: nil, selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
+                        self.uploadAssetsNewAndFull(viewController: nil, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) { }
                     }
                 }
             }
@@ -93,12 +93,14 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     
     // MARK: -
     
-    @objc func initAutoUpload(viewController: UIViewController?) {
+    @objc func initAutoUpload(viewController: UIViewController?, completion: @escaping ()->()) {
         if let account = NCManageDatabase.shared.getAccountActive() {
             if account.autoUpload {
                 NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
                     if hasPermission {
-                        self.uploadAssetsNewAndFull(viewController:viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
+                        self.uploadAssetsNewAndFull(viewController:viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUpload) {
+                            completion()
+                        }
                         if account.autoUploadBackground {
                             NCAskAuthorization.shared.askAuthorizationLocationManager() { (hasFullPermissions) in
                                 if hasFullPermissions {
@@ -112,23 +114,27 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
                     } else {
                         NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
                         self.stopSignificantChangeUpdates()
+                        completion()
                     }
                 }
+            } else {
+                completion()
             }
         } else {
             stopSignificantChangeUpdates()
+            completion()
         }
     }
     
     @objc func autoUploadFullPhotos(viewController: UIViewController?) {
         NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
             if hasPermission {
-                self.uploadAssetsNewAndFull(viewController: viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll)
+                self.uploadAssetsNewAndFull(viewController: viewController, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll) { }
             }
         }
     }
     
-    private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String) {
+    private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String, completion: @escaping ()->()) {
         
         if appDelegate.account == nil || appDelegate.account.count == 0 { return }
         guard let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }