Browse Source

fix download in background

marinofaggiana 3 years ago
parent
commit
8de73d8425
2 changed files with 25 additions and 1 deletions
  1. 4 1
      iOSClient/AppDelegate.swift
  2. 21 0
      iOSClient/Networking/NCNetworking.swift

+ 4 - 1
iOSClient/AppDelegate.swift

@@ -250,7 +250,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
         
         // Clear operation queue
         NCOperationQueue.shared.cancelAllQueue()
-        
+        // Clear download
+        NCNetworking.shared.cancelAllDownloadTransfer()
+
         // Clear older files
         let days = CCUtility.getCleanUpDay()
         if let directory = CCUtility.getDirectoryProviderStorage() {
@@ -283,6 +285,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterD
     // L'applicazione terminerà
     func applicationWillTerminate(_ application: UIApplication) {
         
+        NCNetworking.shared.cancelAllDownloadTransfer()
         NCCommunicationCommon.shared.writeLog("bye bye")
     }
     

+ 21 - 0
iOSClient/Networking/NCNetworking.swift

@@ -815,6 +815,27 @@ import Queuer
         NCOperationQueue.shared.downloadCancelAll()
         #endif
     }
+    
+    func cancelAllDownloadTransfer() {
+       
+        let metadatas = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status != %d", NCGlobal.shared.metadataStatusNormal))
+        
+        for metadata in metadatas {
+
+            if (metadata.status == NCGlobal.shared.metadataStatusWaitDownload || metadata.status == NCGlobal.shared.metadataStatusDownloadError) {
+                
+                NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, session: "", sessionError: "", sessionSelector: "", sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusNormal)
+            }
+            
+            if metadata.status == NCGlobal.shared.metadataStatusDownloading && metadata.session == NCCommunicationCommon.shared.sessionIdentifierDownload {
+                NCNetworking.shared.cancelDownload(ocId: metadata.ocId, serverUrl: metadata.serverUrl, fileNameView: metadata.fileNameView)
+            }
+        }
+        
+        #if !EXTENSION
+        NCOperationQueue.shared.downloadCancelAll()
+        #endif
+    }
         
     //MARK: - WebDav Read file, folder