marinofaggiana 4 years ago
parent
commit
4e58a7c8b3
2 changed files with 78 additions and 0 deletions
  1. 2 0
      iOSClient/AppDelegate.h
  2. 76 0
      iOSClient/AppDelegate.m

+ 2 - 0
iOSClient/AppDelegate.h

@@ -115,6 +115,8 @@
 
 @property (nonatomic) UIUserInterfaceStyle preferredUserInterfaceStyle API_AVAILABLE(ios(12.0));
 
+@property (nonatomic, strong) NSMutableArray *sessionPendingStatusInUpload;
+
 // Shares
 @property (nonatomic, strong) NSArray *shares;
 

+ 76 - 0
iOSClient/AppDelegate.m

@@ -85,6 +85,7 @@
     self.arrayMoveServerUrlTo = [NSMutableArray new];
     self.arrayCopyMetadata = [NSMutableArray new];
     self.arrayCopyServerUrlTo = [NSMutableArray new];
+    self.sessionPendingStatusInUpload = [NSMutableArray new];
     
     // Push Notification
     [application registerForRemoteNotifications];
@@ -238,6 +239,9 @@
         [[NCService sharedInstance] middlewarePing];
     }
     
+    // verify task (download/upload) lost
+    [self verifyTaskLos];
+    
     // Brand
 #if defined(HC)
     tableAccount *account = [[NCManageDatabase sharedInstance] getAccountActive];
@@ -1533,6 +1537,21 @@
         }
     }
     
+    // Upload in pending
+    //
+    NSArray *metadatasInUpload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"session != %@ AND status == %d AND sessionTaskIdentifier == 0", k_upload_session_extension, k_metadataStatusInUpload] sorted:nil ascending:true];
+    for (tableMetadata *metadata in metadatasInUpload) {
+        if ([self.sessionPendingStatusInUpload containsObject:metadata.ocId]) {
+            metadata.status = k_metadataStatusWaitUpload;
+            (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
+        } else {
+            [self.sessionPendingStatusInUpload addObject:metadata.ocId];
+        }
+    }
+    if (metadatasInUpload.count == 0) {
+        [self.sessionPendingStatusInUpload removeAllObjects];
+    }
+    
     // Start Timer
     _timerProcessAutoDownloadUpload = [NSTimer scheduledTimerWithTimeInterval:k_timerProcessAutoDownloadUpload target:self selector:@selector(loadAutoDownloadUpload) userInfo:nil repeats:YES];
 }
@@ -1544,6 +1563,63 @@
     }
 }
 
+- (void)verifyTaskLos
+{
+    // Verify internal error download (lost task)
+    //
+    NSArray *matadatasInDownloading = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"status == %d", k_metadataStatusDownloading] sorted:nil ascending:true];
+    for (tableMetadata *metadata in matadatasInDownloading) {
+        
+        NSURLSession *session = [[CCNetworking sharedNetworking] getSessionfromSessionDescription:metadata.session];
+        
+        [session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
+            
+            NSURLSessionTask *findTask;
+            
+            for (NSURLSessionTask *task in downloadTasks) {
+                if (task.taskIdentifier == metadata.sessionTaskIdentifier) {
+                    findTask = task;
+                }
+            }
+            
+            if (!findTask) {
+                
+                metadata.sessionTaskIdentifier = k_taskIdentifierDone;
+                metadata.status = k_metadataStatusWaitDownload;
+                
+                (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
+            }
+        }];
+    }
+
+    // Verify internal error upload (lost task)
+    //
+    NSArray *metadatasUploading = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"session != %@ AND status == %d", k_upload_session_extension, k_metadataStatusUploading] sorted:nil ascending:true];
+    for (tableMetadata *metadata in metadatasUploading) {
+        
+        NSURLSession *session = [[CCNetworking sharedNetworking] getSessionfromSessionDescription:metadata.session];
+        
+        [session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
+            
+            NSURLSessionTask *findTask;
+            
+            for (NSURLSessionTask *task in uploadTasks) {
+                if (task.taskIdentifier == metadata.sessionTaskIdentifier) {
+                    findTask = task;
+                }
+            }
+            
+            if (!findTask) {
+                
+                metadata.sessionTaskIdentifier = k_taskIdentifierDone;
+                metadata.status = k_metadataStatusWaitUpload;
+                
+                (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
+            }
+        }];
+    }
+}
+
 #pragma --------------------------------------------------------------------------------------------
 #pragma mark ===== OpenURL  =====
 #pragma --------------------------------------------------------------------------------------------