How do I rename a document file with an open UIDocument without closing and reopening the document? Closing (saving), moving, and reopening the document takes too long.
I have the following code that moves the file:
NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateWritingItemAtURL:oldPath
options:NSFileCoordinatorWritingForMoving
writingItemAtURL:newPath
options:NSFileCoordinatorWritingForReplacing
error:&coordinatorError
byAccessor:^(NSURL *newURL1, NSURL *newURL2) {
// Rename the file.
NSFileManager* fileManager = [NSFileManager defaultManager];
[fileCoordinator itemAtURL:oldPath willMoveToURL:newPath];
[fileManager moveItemAtURL:newURL1 toURL:newURL2 error:nil];
[fileCoordinator itemAtURL:oldPath didMoveToURL:newPath];
}];
According to the documentation UIDocument
implements presentedItemDidMoveToURL:
to update its fileURL
(see https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIDocument_Class/)
However, when the above code is called with the document open, it hangs, presumably waiting for a lock on oldPath
(-[NSFileCoordinator(NSPrivate) _blockOnAccessClaim:]
). Works fine if there is no living UIDocument
.
Related, top answer suggests to close and reopen the UIDocument
: What is the proper way to move a UIDocument to a new location on the file-system
The solution was to use a background queue to avoid the deadlock:
dispatch_async(dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0), ^(void) {
NSError *coordinatorError = nil;
NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateWritingItemAtURL:oldPath
options:NSFileCoordinatorWritingForMoving
writingItemAtURL:newPath
options:NSFileCoordinatorWritingForReplacing
error:&coordinatorError
byAccessor:^(NSURL *newURL1, NSURL *newURL2) {
// Rename the file.
NSFileManager* fileManager = [NSFileManager defaultManager];
[fileCoordinator itemAtURL:oldPath willMoveToURL:newPath];
[fileManager moveItemAtURL:newURL1 toURL:newURL2 error:nil];
[fileCoordinator itemAtURL:oldPath didMoveToURL:newPath];
}];
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With