我正在尝试将pdf阅读器应用中的书签同步到服务器。书签将保存到核心数据,然后同步到服务器。
但是,在尝试使用restkit同步核心数据的同时尝试同步到核心数据时,我遇到了异常。我得到的错误:
CoreData could not fulfill a fault for '0x11940014b <x-coredata://5F7F2CE2-4C88-4D87-9C96-B475DC029FCA/Bookmark/p1125>'
单击删除按钮时调用的代码:
- (BOOL)removeBookmarkForPage:(NSUInteger)page inBook:(IBBook *)book
{
__block BOOL isSuccess = NO;
dispatch_sync(_bookmarkQueue, ^{
[[[IBCoreDataManager sharedManager] moc] performBlockAndWait:^() {
NSMutableSet *bookmarks = [_bookmarkCache objectForKey:book.isbn];
[bookmarks enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
IBBookmark *bookmark = (IBBookmark *)obj;
if ([bookmark.pageNumber integerValue] == page) {
bookmark.action = kAnnotationIsRemovedKey;
}
}];
[_bookmarkCache setObject:bookmarks forKey:book.isbn];
}];
});
[[NSNotificationCenter defaultCenter] postNotificationName:IBBookmarksDidChangeNotification object:self];
return isSuccess;
}
bookmark.action = kAnnotationIsRemovedKey;
行是投射异常的行。
只有在发生同步时才会转换异常(调用下面的方法)。在同步时,它会将缓存保存到coredata:
- (BOOL)saveAllBookmarksToCoreDataForBook:(IBBook *)book withError:(NSError **)error
{
__block BOOL isSuccess = YES;
dispatch_sync(_bookmarkQueue, ^{
[[[IBCoreDataManager sharedManager] moc] performBlockAndWait:^() {
NSMutableSet *bookmarksInCache = [_bookmarkCache objectForKey:book.isbn];
NSMutableSet *annotations = [NSMutableSet set];
if (bookmarksInCache) {
[book.userAnnotations enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {
if (![obj isKindOfClass:[IBBookmark class]]) {
[annotations addObject:obj];
}
}];
[annotations unionSet:bookmarksInCache];
book.userAnnotations = annotations;
}
NSError *coreDataError;
[[IBCoreDataManager sharedManager] saveToPersistantStoreWithError:&coreDataError];
if (coreDataError) {
isSuccess = NO;
*error = coreDataError;
}
}];
});
return isSuccess;
}
@end
_bookmarkQueue
定义为:
_bookmarkQueue = dispatch_queue_create([[NSString stringWithFormat:@"myAppName.%@", self] UTF8String], DISPATCH_QUEUE_SERIAL);
_moc
定义为:
_moc = self.objectManager.managedObjectStore.mainQueueManagedObjectContext;
关于如何解决的任何想法?
更新
我也遇到了这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Illegal attempt to establish a relationship 'userAnnotations' between objects in different contexts (source = <IBBook: 0x10045eb90> (entity: Book; id: 0x240004b <x-coredata://76F2C086-D7DE-4210-B0C1-AA49DC5802D3/Book/p9> ;
destination = <IBBookmark: 0x102fa10b0> (entity: Bookmark; id: 0x22a40014b <x-coredata://76F2C086-D7DE-4210-B0C1-AA49DC5802D3/Bookmark/p2217> ; data: <fault>))'
在book.userAnnotations = annotations;
<{1}} <{1}}行中的