停止 NSFileManager 进程
我正在使用 NSFileManager 复制一些数据:
success = [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:[dirToCopyTo stringByAppendingPathComponent:file] error:©Error];
但是 NSFileManager 似乎无法提供停止此过程的方法。有办法吗?
谢谢
I am copying some data using NSFileManager:
success = [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:[dirToCopyTo stringByAppendingPathComponent:file] error:©Error];
NSFileManager seems to provide no way of stopping this process however. Is there a way?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不适用于
NSFileManager
,不。您必须降至较低级别的FSCopyObjectAsync
函数。有关如何使用此函数的示例,请查看此问题。
至于如何取消复制,您将在复制回调中获取的第一个参数是
FSFileOperationRef
。将该参数作为参数传递给 FSFileOperationCancel() 应取消复制。 (请参阅此 CocoaBuilder.com 线程了解更多信息)Not with
NSFileManager
, no. You have to drop to the lower levelFSCopyObjectAsync
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 toFSFileOperationCancel()
should cancel the copy. (See this CocoaBuilder.com thread for more info)这是一个很老的问题,但我只是偶然发现它,因为我遇到了完全相同的问题;我想使用 NSFileManager 而不是 Carbon 方法并实现手动递归复制,因为需要处理的情况太多,并且 NSFileManager 在解决文件操作中的错误方面做得很好(例如在卷之间移动、移动到已安装的网络路径等) )。基本上,您可以通过引发错误来停止 NSFileManager。它不漂亮,但可以正常工作。例如,我使用 NSFileManager 将目录从一个位置移动到另一个位置,并为用户提供了取消操作的选项;为了解决 NSFileManager 的“不可取消性”问题,我只是在复制目标目录的内容时重命名了目标目录; NSFileManager 引发错误并且复制操作完全停止。不过,这只适用于卷或副本之间的移动(完成复制或副本删除),因为对于卷内的移动,OSX 只是进行重命名,这实际上是瞬时的。
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.
我尝试按照上一篇文章的建议移动文件,但移动不会导致 copyItemAtURL 返回错误。 Apple 似乎已弃用了无法用同等功能替代的方法。
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.