How to know a file is writing via NSFileManager

923 views Asked by At

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 .

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.

4

There are 4 answers

2
Sega-Zero On BEST ANSWER

NSFileCoordinator and NSFilePresenter are created just for that. You may find interesting Advanced iCloud Document Storage video from wwdc that covers the usage of this classes. Building a Document-based App will be great to watch too.

1
Mithun Ravindran On

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)

The dispatch I/O convenience API lets you perform asynchronous read and write operations on file descriptors. This API supports stream-based semantics for accessing the contents of the file-descriptor.

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

2
Lefteris On

Basically you need to check the attributes of the file and particullary the NSFileBusy attribute:

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

Try to fetch data of file by using following code :

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