开发者

document directory problem?

开发者 https://www.devze.com 2022-12-24 20:29 出处:网络
When i write Data(53MB ) to Document directory ,the data is not written when i check directly through application support path.i coded like this,

When i write Data(53MB ) to Document directory ,the data is not written when i check directly through application support path.i coded like this,

   - (BOOL)writeApplicationData:(NSData *)data toFile:(NSString *)fileName
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (开发者_运维百科!documentsDirectory) {
    NSLog(@"Documents directory not found!");
    return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);

} it works fine, but when i read the data using follwing code, the data is null, anyhelp pls?

 - (NSData *)applicationDataFromFile:(NSString *)fileName {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;

}


The code is working for me (on a small download).

Some thoughts, are you downloading the 53MB over the network? Perhaps you're trying to read it before it's finished? The "atomically" flag on write to file says:

If YES, the data is written to a backup file, and then—assuming no errors occur—the backup file is renamed to the name specified by path; otherwise, the data is written directly to path.

If you're downloading this Asynchronously and can't use partial results, you may have to wait for it to complete. Otherwise you can set the atomically:NO and read in the partial result.

0

精彩评论

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