我正在尝试将文件写入 GCD 中的路径,但它不起作用。不断收到错误“NSUnderlyingError=0x6a3d450”操作无法完成。是目录”
dispatch_queue_t downloadQueue = dispatch_queue_create("flickr downloader", NO);
dispatch_async(downloadQueue, ^{
NSString* name = [photoDictionary valueForKey:@"title"];
path = [path stringByAppendingPathComponent:id];
if(![manager fileExistsAtPath:path]){
if([manager createDirectoryAtPath: path withIntermediateDirectories:NO attributes:nil error:nil]){ //create intermediate directories
data = [NSData dataWithContentsOfURL:[FlickrFetcher urlForPhoto:photoDictionary format:FlickrPhotoFormatOriginal]];
NSError *error = nil;
BOOL success = [data writeToFile:path options:NSDataWritingAtomic error:&error];
if (!success) {
NSLog(@"Failed to write to file with error: %@", [error description]);
}
if([data writeToFile: path atomically:YES]){
NSLog(@"data written to file");
}
}
}
else{
if([manager fileExistsAtPath:path]){
data = [manager contentsAtPath:path];
}
}
dispatch_async(dispatch_get_main_queue() , ^{
sender.navigationItem.rightBarButtonItem = item;
sender.photoData = data;
sender.photoName = name;
//call the segue
[sender performSegueWithIdentifier:segueTitle sender:sender];
});
});
dispatch_release(downloadQueue);
我遇到的问题是 [data writeToFile: path atomically:YES] 永远不会执行。但是,确实创建了数据路径,当我“po data”时,程序有点挂起,所以我猜想在调用 writeToFile 时数据没有完全获取。但是我无法准确定位问题,也不知道如何解决。任何建议都将不胜感激。谢谢!