Need to see if your shared folder is taking up space on your dropbox 👨‍💻? Find out how to check here.

Forum Discussion

App4G's avatar
App4G
Helpful | Level 6
5 years ago
Solved

Files.download does not start when number of files in folder is > 1000

      I'm having trouble downloading entire folders to a local device via swiftyDropbox. I am doing the ListFolder and ListFolderContinue (which I observe that it chunks it to ~500 files per ...
  • App4G's avatar
    App4G
    5 years ago

    This post https://stackoverflow.com/a/66227963/14414215 greatly helped in my understanding of how semaphore works and how implementing semaphores (besides using dispatchGroups) is able to properly control the files.download calls. 

     

       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)
                // }
            }
          }
        }
       }
      }

     

About Discuss Dropbox Developer & API

Node avatar for Discuss Dropbox Developer & API
Make connections with other developers

The Dropbox Community team is active from Monday to Friday. We try to respond to you as soon as we can, usually within 2 hours.

If you need more help you can view your support options (expected response time for an email or ticket is 24 hours), or contact us on X, Facebook or Instagram.

For more info on available support options for your Dropbox plan, see this article.

If you found the answer to your question in this Community thread, please 'like' the post to say thanks and to let us know it was useful!