【发布时间】:2012-06-11 09:17:23
【问题描述】:
我正在尝试将文件写入 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 时数据没有完全获取。但是我无法准确定位问题,也不知道如何解决。任何建议都将不胜感激。谢谢!
【问题讨论】:
-
“po 路径”怎么样?上面的错误消息清楚地表明它正在尝试写入目录...您需要附加文件名。 “路径”是什么样的?
-
当我的 po 路径我得到类似:/Users/tanchen/Library/Application Support/iPhone Simulator/5.0/Applications/7E26ED55-04F6-4B79-9C65-6A88FACF93C9/Library/Caches/71231321。 .. 71231321 是照片的唯一 ID。问题是错误消息实际上表明该目录已创建。我还将 71231321 更改为其他自定义字符串,例如 mytext.txt 但仍然没有任何运气)=
-
我还将代码块移到 GCD 之外,以消除由它引起的任何并发症,但仍然没有工作/
-
@borrrden 抱歉在之前的 cmets 中忘记@你了
标签: objective-c ios cocoa grand-central-dispatch writetofile