开发者

Stop NSFileManager process

I am copying some data using NSFileManager:

success = [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:[dirToCopyTo stringByAppendingPathComponent:file] error:&copyEr开发者_StackOverflow中文版ror];

NSFileManager seems to provide no way of stopping this process however. Is there a way?

Thanks


Not with NSFileManager, no. You have to drop to the lower level FSCopyObjectAsync function.

For an example on how to use this function, check out this question.

As for how to cancel the copy, the first parameter you're going to get to your copy callback is an FSFileOperationRef. Passing that parameter as the argument to FSFileOperationCancel() should cancel the copy. (See this CocoaBuilder.com thread for more info)


This is a pretty old question but I just stumbled upon it since I had the exact same problem; I wanted to use NSFileManager over Carbon methods and implementing a manual recursive copy, because there's just too many cases to handle, and NSFileManager does a good job of resolving errors in file operations (e.g. move between volumes, move to a mounted network path, etc). You can stop NSFileManager, basically, by inducing an error. It's not pretty, but it works properly. E.g. I used NSFileManager to move a directory from one location to another, and I gave the user the option to cancel the operation; to workaround the NSFileManager's 'un-cancellability', I just renamed the destination directory while its contents are being copied; NSFileManager raises an error and the copy operation cleanly stops. This will only work for moves between volumes or copies, though (where a copy or copy-delete is done), because for moves within a volume, OSX just does a rename, which is practically instantaneous.


I tried moving the file as the previous post suggested, but the move does not cause the copyItemAtURL to return with an error. It seems that Apple deprecated a method that has no replacement with the equivalent functionality.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜