Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

NSFileCoordinator Swift Concurrency

I'm working on implementing file moving with NSFileCoordinator. I'm using the slightly newer asynchronous API with the NSFileAccessIntents. My question is, how do I go about notifying the coordinator about the item move? Should I simply create a new instance in the asynchronous block? Or does it need to be the same coordinator instance?

let writeQueue = OperationQueue()

public func saveAndMove(data: String, to newURL: URL) {
    let oldURL = presentedItemURL!
    let sourceIntent = NSFileAccessIntent.writingIntent(with: oldURL, options: .forMoving)
    let destinationIntent = NSFileAccessIntent.writingIntent(with: newURL, options: .forReplacing)
    let coordinator = NSFileCoordinator()
    coordinator.coordinate(with: [sourceIntent, destinationIntent], queue: writeQueue) { error in
        if let error {
            return
        }
        
        do {
            // ERROR: Can't access NSFileCoordinator because it is not Sendable (Swift 6)
            coordinator.item(at: oldURL, willMoveTo: newURL)
            try FileManager.default.moveItem(at: oldURL, to: newURL)
            coordinator.item(at: oldURL, didMoveTo: newURL)
        } catch {
            print("Failed to move to \(newURL)")
        }
    }
}
NSFileCoordinator Swift Concurrency
 
 
Q