简体   繁体   English

如何知道文件是通过NSFileManager编写的

[英]How to know a file is writing via NSFileManager

I got a problem , How to know the file is accessible ??? 我有一个问题,如何知道该文件是否可访问???

For example , the file is writing , but not finish yet. 例如,文件正在写入,但尚未完成。 And I check the file again , 我再次检查文件,

I use 我用

[[NSFileManager defaultManager] fileExistsAtPath:filePath]; 

It return true ... but the file is not accessible . 它返回true ...但文件无法访问。

How to determine the file is ready ? 如何确定文件准备好了?

Thanks 谢谢

Webber 韦伯

EDIT Maybe I'm not telling my problem clearly . 编辑也许我没有清楚地告诉我的问题。 I mean if you start transport a video file . 我的意思是如果你开始传输视频文件。 before transport finish . 在运输完成之前。 the video file is not accessible , but you still can get a part of transporting file. 视频文件无法访问,但您仍然可以获取传输文件的一部分。

Write Data in async way using GCD concept and once it is completed, the completion handler will be executed.(Where completion of a writing process shall be detected) 使用GCD概念以异步方式写入数据,一旦完成,将执行完成处理程序。(应检测写入过程的完成)

The dispatch I/O convenience API lets you perform asynchronous read and write operations on file descriptors. 调度I / O便利API允许您对文件描述符执行异步读写操作。 This API supports stream-based semantics for accessing the contents of the file-descriptor. 此API支持基于流的语义,用于访问文件描述符的内容。

Method: 方法:

void dispatch_write ( dispatch_fd_t fd, dispatch_data_t data, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error) );

Ref: https://developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_write 参考: https//developer.apple.com/library/ios/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_write

Basically you need to check the attributes of the file and particullary the NSFileBusy attribute: 基本上你需要检查文件的属性,特别是NSFileBusy属性:

if ([fileManager fileExistsAtPath:filePath]) {
    NSDictionary *attributes = [fileManager attributesOfItemAtPath:filePath error:nil];
    NSLog(@"File Busy %@", [attributes objectForKey:NSFileBusy]);
}

NSFileCoordinator and NSFilePresenter are created just for that. NSFileCoordinatorNSFilePresenter就是为此而创建的。 You may find interesting Advanced iCloud Document Storage video from wwdc that covers the usage of this classes. 您可以从wwdc中找到有趣的高级iCloud文档存储视频,其中包含此类的用法。 Building a Document-based App will be great to watch too. 构建基于文档的应用程序也很棒。

Try to fetch data of file by using following code : 尝试使用以下代码获取文件数据:

NSURL *url = [NSURL fileURLWithPath:filePath];
NSData *readData = [NSData dataWithContentsOfURL:url];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM