In the following code, the string "after" is never printed:
class ViewController: UIViewController, UIDocumentPickerDelegate {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
let documentPicker = UIDocumentPickerViewController(documentTypes: [kUTTypeText as String], in: .open)
documentPicker.delegate = self
present(documentPicker, animated: true)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let url = urls[0]
guard url.startAccessingSecurityScopedResource() else {
return
}
defer {
url.stopAccessingSecurityScopedResource()
}
DispatchQueue.global(qos: .background).async {
var fcError, _error: NSError?
NSFileCoordinator(filePresenter: nil).coordinate(writingItemAt: url, options: [.forMoving], error: &fcError, byAccessor: { url in
print("before")
do {
try FileManager.default.trashItem(at: url, resultingItemURL: nil)
// try FileManager.default.moveItem(at: url, to: url.deletingLastPathComponent().appendingPathComponent("bla"))
} catch {
_error = error as NSError
}
print("after")
})
print(fcError ?? _error)
}
}
}
Other file operations (such as moving a file, see commented out line) work as expected. Commenting out the part with the NSFileCoordinator seems to solve the problem.