當文件夾中的文件數為1000時,Dropbox Files.Download不會啟動 [英] Dropbox Files.download does not start when number of files in folder is > 1000

查看:0
本文介紹了當文件夾中的文件數為1000時,Dropbox Files.Download不會啟動的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在Dropbox論壇上交叉發布了我最初的問題。我認為在這里為快速下載框的用戶提供這一功能也是一件好事。

我無法通過wiftyDropbox將整個文件夾下載到本地設備。 我正在執行ListFold和ListFolderContinue(我觀察到每個響應將其分塊為大約500個文件),并將其附加到本地數組。

之后,我將此數組傳遞給文件。下載。但是,我發現如果我的文件夾是>;1000個文件(txt文件大小約為0.5-1KB),下載過程將不會開始。

static func downloadMissingFiles(client: DropboxClient, callingProcess: String) {
      let fileManager = FileManager.default
      let localBaseURL = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("Cloud/Dropbox", isDirectory: true)
      
      // Data will be in the form of
      // key   : "/workouts/workout list 1/peye.mrc"
      // value : "//workouts/workout list 1/peye.mrc=_-_=015ca880b135d01000000020cb26de0"
      for dbFiles in Array(dbFileNameRevDict) {
        let dbFilePathLower = dbFiles.key
        let dbFileNameRev = dbFiles.value
        let fullURL = localBaseURL.appendingPathComponent(dbFileNameRev)
        
        if fileManager.fileExists(atPath: fullURL.path) {
          print("  -> FILE EXISTS dbFileNameRev:(dbFileNameRev)")
          localFileList.append(dbFileNameRev)
        } else {
          let destination : (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
            return fullURL
          }
          
          client.files.download(path:dbFilePathLower, overwrite: true, destination: destination)
            .response { response, error in
              if let (_, url) = response {
                print("====> DOWNLOADED:(url.lastPathComponent)")
              } else if let error = error {
               print(error)
            }
            /// This gives a progress of every single file on it's own. Hence, useless
            // .progress { progressData in
            //  print(progressData)
            // }
        }
      }
    }
  }

我已經嘗試了各種方法來下載這些文件,我也嘗試了一個串行隊列來逐個迭代文件數組,但都不起作用。

這就是我在查看hasmore屬性時處理ListFold和ListFolderContinue的方式。

      // https://stackoverflow.com/a/52870045/14414215
      if result.hasMore == true {
        processDBMore(client: client, cursor: result.cursor)
      } else {
        // When there is no more files (as indicated by hasMore == false)
        // start downloading the files
        downloadMissingFiles(client: client, callingProcess: "processDBMore-Finish")
        print("PrcessDBMore - dropboxGroup.leave")
        dropboxGroup.leave()
      }

推薦答案

根據Greg(SwiftyDropbox)

每個‘client.files.download’調用通過創建一個文件來下載一個文件 向Dropbox API服務器發出的HTTPS請求。此外,這些呼叫 異步運行,并且在調用完成之前不會阻塞。那 是,調用‘client.files.download’將啟動HTTPS請求,但是 將在完成之前返回,并且響應已完全完成 收到了。(一旦請求完成,它只運行提供的塊。) 在這種情況下,在您這里展示的代碼中,您實際上是 幾乎同時啟動1000個連接,所以它是 可能會耗盡您的網絡連接。您應該更新您的代碼 一次只提交其中的一個(或幾個)。你提到過你 已嘗試使用串行隊列,但可能遇到相同的問題, 因為實際請求是異步運行的。

所以我在尋找其他解決方案時,看到了這篇https://stackoverflow.com/a/66227963/14414215,它對我理解信號量是如何工作的,以及實現信號量(除了使用DispatchGroups之外)如何能夠正確地控制文件。下載調用有很大幫助。

   static func downloadMissingFiles(client: DropboxClient, callingProcess: String) {
      let fileManager = FileManager.default
      let localBaseURL = fileManager.urls(for: .cachesDirectory, in: .userDomainMask)[0].appendingPathComponent("Cloud/Dropbox", isDirectory: true)
      let semaphore = DispatchSemaphore(value: 1)  // insert desired concurrent downloads value here.

      // Data will be in the form of
      // key   : "/workouts/workout list 1/peye.mrc"
      // value : "//workouts/workout list 1/peye.mrc=_-_=015ca880b135d01000000020cb26de0"
      DispatchQueue.global().async { // Wrap the call within an async block
      for dbFiles in Array(dbFileNameRevDict) {
        semaphore.wait() // Decrement the semaphore counter
        let dbFilePathLower = dbFiles.key
        let dbFileNameRev = dbFiles.value
        let fullURL = localBaseURL.appendingPathComponent(dbFileNameRev)
        
        if fileManager.fileExists(atPath: fullURL.path) {
          print("  -> FILE EXISTS dbFileNameRev:(dbFileNameRev)")
          localFileList.append(dbFileNameRev)
          semaphore.signal()  // Increment semaphore counter
        } else {
          let destination : (URL, HTTPURLResponse) -> URL = { temporaryURL, response in
            return fullURL
          }
          
          client.files.download(path:dbFilePathLower, overwrite: true, destination: destination)
            .response { response, error in
              if let (_, url) = response {
                print("====> DOWNLOADED:(url.lastPathComponent)")
                // we've reached here means we've successfully download the file
                // So we can (release)increment semaphore counter
                semaphore.signal() 
              } else if let error = error {
               print(error)
            }
            /// This gives a progress of every single file on it's own. Hence, useless
            // .progress { progressData in
            //  print(progressData)
            // }
        }
      }
    }
   }
  }

這篇關于當文件夾中的文件數為1000時,Dropbox Files.Download不會啟動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持IT屋!

查看全文
登錄 關閉
掃碼關注1秒登錄
發送“驗證碼”獲取 | 15天全站免登陸
全免费A级毛片免费看无码播放