swifticloudnsfilemanager

I am trying to run a function when ubiquitousItemDownloadingStatusKey is 'Current'


I am trying to download a directory in my app's iCloud ubiquity container and I would like to call a function when it finished downloading. I can get the status like this:

func synciCloud(){
    do { try FileManager.default.startDownloadingUbiquitousItem(at: documentsPath)
        do {
            let status = try documentsPath.resourceValues(forKeys: [.ubiquitousItemDownloadingStatusKey])
            print(status)
        }
        catch let error { print("Failed to get status: \(error.localizedDescription)") }
    }
    catch let error { print("Failed to download iCloud Documnets Folder: \(error.localizedDescription)") }
}

The console prints:

URLResourceValues(_values: [__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey): NSURLUbiquitousItemDownloadingStatusCurrent], _keys: Set([__C.NSURLResourceKey(_rawValue: NSURLUbiquitousItemDownloadingStatusKey)]))

What I am confused about is the syntax of NSMetadata and how to execute my function. My first thought was to do something like the code below but I'm not sure how to go about it:

if status.ubiquitousItemDownloadingStatus == ???? { function() }

Any suggestions would be appreciated.

Thank you.


Solution

  • The value of status.ubiquitousItemDownloadingStatus is URLUbiquitousItemDownloadingStatus. That has three values. .current would indicate the file is up-to-date.

    if status.ubiquitousItemDownloadingStatus == .current {
        // it's up-to-date
    }