1

I have implemented the backup system like we have in WhatsApp.

  1. First, it is checking for the document directory and zip the data to temporary directory.
  2. Then get the url to iCloud

    func getUrlToiCloud() -> URL? {
    let fileManager = FileManager.default
    let url = fileManager.url(forUbiquityContainerIdentifier: nil)
    return url
    }
    
  3. Next is to backup the items to iCloud

    func iCloudBackupHandler(_ backupName: String, backupPath: String) {
    guard let url = getUrlToiCloud()  else {
        return
    }
    
    let loggedInUser: String
    if #available(iOS 10.0, *) {
        loggedInUser = CKCurrentUserDefaultName
    } else {
        loggedInUser = CKOwnerDefaultName
    }
    print("loggedInUser:\(loggedInUser)")
    let localBackupUrl = URL(fileURLWithPath: backupPath)
    let iCloudDocumentUrl = url.appendingPathComponent(backupName)
    print("Local:\(localBackupUrl)")
    print("iCloudDocumentUrl:\(String(describing: iCloudDocumentUrl))")
    do {
        try FileManager.default.replaceItem(at: iCloudDocumentUrl, withItemAt: localBackupUrl, backupItemName: backupName, options: .withoutDeletingBackupItem, resultingItemURL: nil)
       //try FileManager.default.setUbiquitous(true, itemAt: localBackupUrl, destinationURL: iCloudDocumentUrl)
    } catch {
        print("error while backup: \(error)")
    }
    }
    

    My question is here, on the following line. which backups the item to iCloud.

     try FileManager.default.replaceItem(at: iCloudDocumentUrl, withItemAt: localBackupUrl, backupItemName: backupName, options: .withoutDeletingBackupItem, resultingItemURL: nil)
    

How can I track the progress of the file being uploaded to iCloud? As in WhatsApp there is a progress(percentage) of the file being backed up. Please let me know, if you have idea on this.

Bhavin Kansagara
  • 2,866
  • 1
  • 16
  • 20
  • Possible duplicate of [update progress bar during copy file with NSFileManager](https://stackoverflow.com/questions/17544145/update-progress-bar-during-copy-file-with-nsfilemanager) – Larme Sep 18 '17 at 08:59
  • Might be better to use `URLSessionDownloadTask`, that way you have a way to determine progress. You also have the ability to make it a long running task and do things without blocking the main thread. Also allows download when app is in background mode – user1046037 Sep 18 '17 at 13:47
  • That's true for the download part, but this is related to iCloud upload progress. it will have some different approach. may be like the link reference by @Larme in above comment. – Bhavin Kansagara Sep 18 '17 at 14:15

0 Answers0