I have the following function which deletes a file (happens to be iCloud). Sometimes it works, other times it hangs at the filecoordinate.coordinate line:
func deleteDocumentAtURL(url: NSURL) {
print("IN DELETE: existing url: \(url)")
print("IN DELETE: openable: \(itemIsOpenable(url as URL))")
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
print("IN DELETE: attempting file coordinator, might get STUCK here")
print("IN DELETE: file coordinator = \(fileCoordinator)")
var fileError: NSError?
fileCoordinator.coordinate(writingItemAt: url as URL, options: .forDeleting, error: &fileError, byAccessor: { (urlForModifying) -> Void in
print("IN DELETE: modifying URL: \(urlForModifying)")
do {
print("IN DELETE deleteDocumentBlock")
try FileManager.default.removeItem(at: urlForModifying)
print("IN DELETE: deletion OK")
} catch let error as NSError {
let alert = UIAlertController(title: "Error deleting", message: error.localizedDescription, preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Done", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
print("Error code = \(fileError)")
print("IN DELETE: at the end")
}
the itemIsOpenable function checks the iCloud download status as follows:
func itemIsOpenable(_ url:URL?) -> Bool {
// Return false if item is nil
guard let itemURL = url else {
return false
}
// Return true if we don't have access to iCloud (which means
// that it's not possible for it to be in conflict - we'll always have
// the latest copy)
if DocumentListViewController.iCloudAvailable == false {
return true
}
// Ask the system for the download status
var downloadStatus : AnyObject?
do {
try (itemURL as NSURL).getResourceValue(&downloadStatus,
forKey: URLResourceKey.ubiquitousItemDownloadingStatusKey)
} catch let error as NSError {
NSLog("Failed to get downloading status for \(itemURL): \(error)")
// If we can't get that, we can't open it
return false
}
print("item is openable download status = \(downloadStatus)")
// Return true if this file is the most current version
if downloadStatus as? String == URLUbiquitousItemDownloadingStatus.current.rawValue {
print("download status = \(downloadStatus)")
return true
} else {
let currentStatus = downloadStatus as? String
print("current status = \(currentStatus)")
print("Ubiq status = \(URLUbiquitousItemDownloadingStatus.current.rawValue)")
return false
}
}
The print output when the app hangs is as follows (it goes no further):
IN DELETE: existing url: file:///private/var/mobile/Library/Mobile%20Documents/iCloud~net~thesavorys~Notes/Documents/Document%201997293854.note
item is openable download status = Optional(NSURLUbiquitousItemDownloadingStatusCurrent) download status = Optional(NSURLUbiquitousItemDownloadingStatusCurrent) IN DELETE: openable: true IN DELETE: attempting file coordinator, might get STUCK here IN DELETE: file coordinator = NSFileCoordinator: 0x17046de40
Any thoughts of what is happening here much appreciated...
Thanks Tim