【发布时间】:2014-04-22 19:49:59
【问题描述】:
我已经搜索了过去几个小时,但还没有找到答案。我实现了一个 AVFoundation 相机,我将图像数据保存到磁盘并仅将路径存储在核心数据中。一切正常,但在随机拍摄照片后出现此错误:
CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获到异常。这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的一个错误。 -[__NSDatelocalizedCaseInsensitiveCompare:]:无法识别的选择器发送到实例
这是我的获取请求代码:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Photo"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"notebook = %@", notebookItem];
[request setPredicate:predicate];
request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"dateTaken"
ascending:NO
selector:@selector(compare:)]];
这是我如何将数据保存在单独的类中:
//I create a photo Object to save to core data and set properties like dateTaken and tittle
//Here is where I create the path name
NSDate *dateString = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS"];
NSString *path = [formatter stringFromDate:dateString];
//Now I save to disk
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// the path to write file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:path];
NSLog(@"path: %@", path);
[[[self captureManager] ImageDataObject] writeToFile:filePath atomically:YES];
photo.imagePath = filePath;
[notebookItem addPhotosObject:photo];
[self.SharedDocumentHandlerInstance saveDocument];
我追踪了崩溃点,它发生在我保存文档的代码行中(上面的最后一个),但可能是获取请求导致它。我还在我的表中设置了 self.fetchedResultsController.delegate = nil。同样,在拍摄随机数量的照片后会发生此错误。提前感谢您的帮助!
【问题讨论】:
-
最好的猜测是您违反了 CoreData 的多线程限制,并试图从后台线程修改 CoreData。
-
我也这么认为,可能会自动设置:是的(我没有使用任何线程)。
-
您正在使用 AVFoundation,因此您正在使用线程。 AVFoundation 回调在后台线程上进行。 atomically:YES 不是问题,只是改变了文件的写出方式(它写入 temp,然后将 temp 移动到 name)
-
那么我怎样才能在主线程中进行保存。并感谢您的帮助!
-
许多不同的方法,最好的办法可能是在 SO 中搜索 CoreData 背景和/或挖掘 Apple CoreData 并发文档,该文档对所有方法进行了很好的审查。对于您的情况,最简单的可能是
-[NSManagedObjectContext performBlockAndWait:]
标签: ios objective-c core-data avfoundation unrecognized-selector