marinofaggiana 4 years ago
parent
commit
02c2039c3f

+ 0 - 5
iOSClient/AppDelegate.h

@@ -44,7 +44,6 @@
 @interface AppDelegate : UIResponder <UIApplicationDelegate, UNUserNotificationCenterDelegate>
 
 // Timer Process
-@property (nonatomic, strong) NSTimer *timerProcessAutoUpload;
 @property (nonatomic, strong) NSTimer *timerUpdateApplicationIconBadgeNumber;
 @property (nonatomic, strong) NSTimer *timerErrorNetworking;
 
@@ -137,10 +136,6 @@
 - (void)settingThemingColorBrand;
 - (void)changeTheming:(UIViewController *)viewController tableView:(UITableView *)tableView collectionView:(UICollectionView *)collectionView form:(BOOL)form;
 
-// Task Networking
-- (void)loadAutoUpload;
-- (void)startLoadAutoUpload;
-
 // Maintenance Mode
 - (void)maintenanceMode:(BOOL)mode;
 

+ 5 - 13
iOSClient/AppDelegate.m

@@ -113,7 +113,6 @@
     }
         
     // Start Timer
-    self.timerProcessAutoUpload = [NSTimer scheduledTimerWithTimeInterval:k_timerProcessAutoUpload target:self selector:@selector(loadAutoUpload) userInfo:nil repeats:YES];
     self.timerUpdateApplicationIconBadgeNumber = [NSTimer scheduledTimerWithTimeInterval:k_timerUpdateApplicationIconBadgeNumber target:self selector:@selector(updateApplicationIconBadgeNumber) userInfo:nil repeats:YES];
     [self startTimerErrorNetworking];
 
@@ -159,6 +158,11 @@
         [self passcodeWithAutomaticallyPromptForBiometricValidation:true];
     });
     
+    // Auto upload
+    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
+        [NCNetworking.shared loadAutoUpload];
+    });
+    
     return YES;
 }
 
@@ -1046,8 +1050,6 @@
     
     NSLog(@"%@", [NSString stringWithFormat:@"[LOG] PROCESS-AUTO-UPLOAD %ld - %@", counterUpload, [CCUtility transformedSize:sizeUpload]]);
     
-    // Stop Timer
-    [_timerProcessAutoUpload invalidate];
         
     // ------------------------- <selector Upload> -------------------------
     
@@ -1217,16 +1219,6 @@
         
         [[NCUtility sharedInstance] deleteAssetLocalIdentifiersWithAccount:self.activeAccount sessionSelector:selectorUploadAutoUpload];
     }
-    
-    // Start Timer
-    _timerProcessAutoUpload = [NSTimer scheduledTimerWithTimeInterval:k_timerProcessAutoUpload target:self selector:@selector(loadAutoUpload) userInfo:nil repeats:YES];
-}
-
-- (void)startLoadAutoUpload
-{
-    if (self.timerProcessAutoUpload.isValid) {
-        [self performSelectorOnMainThread:@selector(loadAutoUpload) withObject:nil waitUntilDone:YES];
-    }
 }
 
 #pragma --------------------------------------------------------------------------------------------

+ 1 - 6
iOSClient/Main/CCMain.m

@@ -781,7 +781,6 @@
                     } else {
                         
                         [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
-                        [appDelegate startLoadAutoUpload];
                     }
 
                 } else {
@@ -1088,8 +1087,6 @@
         
         [[NCManageDatabase sharedInstance] addMetadatas:metadatasNOConflict];
         [[NCManageDatabase sharedInstance] addMetadatas:metadatasMOV];
-        
-        [appDelegate startLoadAutoUpload];
     }
 }
 
@@ -1855,9 +1852,7 @@
                 [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
             }
         }
-    }
-        
-    [appDelegate startLoadAutoUpload];
+    }        
 }
 
 #pragma mark -

+ 0 - 1
iOSClient/Main/Create cloud/NCCreateFormUploadConflict.swift

@@ -228,7 +228,6 @@ extension NCCreateFormUploadConflictDelegate {
         } else {
             
             NCManageDatabase.sharedInstance.addMetadatas(metadatasNOConflict)
-            appDelegate.startLoadAutoUpload()            
         }
                 
         dismiss(animated: true)

+ 0 - 2
iOSClient/Main/Create cloud/NCCreateFormUploadScanDocument.swift

@@ -563,8 +563,6 @@ class NCCreateFormUploadScanDocument: XLFormViewController, NCSelectDelegate, NC
         }
         
         NCManageDatabase.sharedInstance.addMetadata(metadata)
-
-        self.appDelegate.startLoadAutoUpload()
                         
         // Request delete all image scanned
         let alertController = UIAlertController(title: "", message: NSLocalizedString("_delete_all_scanned_images_", comment: ""), preferredStyle: .alert)

+ 1 - 3
iOSClient/Main/Create cloud/NCCreateFormUploadVoiceNote.swift

@@ -266,9 +266,7 @@ class NCCreateFormUploadVoiceNote: XLFormViewController, NCSelectDelegate, AVAud
         CCUtility.copyFile(atPath: self.fileNamePath, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
                    
         NCManageDatabase.sharedInstance.addMetadata(metadata)
-        
-        self.appDelegate.startLoadAutoUpload()
-        
+                
         self.dismiss(animated: true, completion: nil)
     }
     

+ 47 - 0
iOSClient/Networking/NCNetworking.swift

@@ -469,6 +469,53 @@ import Alamofire
         }
     }
     
+    @objc func loadAutoUpload() {
+
+        let inBackground = UIApplication.shared.applicationState == .background
+        
+        DispatchQueue.global().async {
+            
+            var counterUpload = 0
+            var sizeUpload = 0
+            var maxConcurrentOperationUpload = k_maxConcurrentOperation
+        
+            let metadatasUpload = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", k_metadataStatusInUpload, k_metadataStatusUploading), freeze: true)
+            counterUpload = metadatasUpload.count
+            
+            for metadata in metadatasUpload {
+                sizeUpload = sizeUpload + Int(metadata.size)
+            }
+            
+            debugPrint("[LOG] PROCESS-AUTO-UPLOAD \(counterUpload)")
+       
+            // ------------------------- <selector Upload> -------------------------
+            
+            while counterUpload < maxConcurrentOperationUpload {
+                if sizeUpload > k_maxSizeOperationUpload { break }
+                var predicate = NSPredicate()
+                
+                if inBackground {
+                    predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d AND typeFile != %@", selectorUploadFile, k_metadataStatusWaitUpload, k_metadataTypeFile_video)
+                } else {
+                    predicate = NSPredicate(format: "sessionSelector == %@ AND status == %d", selectorUploadFile, k_metadataStatusWaitUpload)
+                }
+                
+                if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: predicate, freeze: true) {
+                    NCManageDatabase.sharedInstance.setMetadataSession(ocId: metadata.ocId, status: Int(k_metadataStatusInUpload))
+                    self.upload(metadata: metadata, background: true) { (_, _) in }
+                    counterUpload += 1
+                    sizeUpload = sizeUpload + Int(metadata.size)
+                } else {
+                    break
+                }
+            }
+            
+            DispatchQueue.main.asyncAfter(deadline: .now() + TimeInterval(k_timerProcessAutoUpload)) {
+                self.loadAutoUpload()
+            }
+        }
+    }
+
     //MARK: - Download / Upload
     
     @objc func verifyTransfer() {

+ 1 - 1
iOSClient/Networking/NCNetworkingNotificationCenter.swift

@@ -179,7 +179,7 @@ import Foundation
                                                 
                 if metadata.account == appDelegate.activeAccount {
                     if errorCode == 0 {
-                        appDelegate.startLoadAutoUpload()
+                        //appDelegate.startLoadAutoUpload()
                     } else {
                         if errorCode != -999 && errorCode != 401 && errorDescription != "" {
                             NCContentPresenter.shared.messageNotification("_upload_file_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)

+ 0 - 2
iOSClient/UploadFromOtherUpp/CCUploadFromOtherUpp.m

@@ -181,8 +181,6 @@
     
     // Add Medtadata for upload
     [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
-
-    [appDelegate startLoadAutoUpload];
     
     [self dismissViewControllerAnimated:YES completion:nil];
 }