Aly*_*hak 7 iphone core-data ipad
当在iPhone(ios 3.1)上运行以下代码时,删除后获取的对象的数量比删除前少一个.但在iPad(ios 3.2)上,计数仍然保持不变.这种不一致导致iPad崩溃,因为代码中的其他地方,删除后不久,调用fetchedObjects并且信任计数的调用代码尝试访问刚刚删除的对象的属性,从而导致NSObjectInaccessibleException错误(见下文) ).一个修复方法是使用对tryFeetch的注释调用,当执行该调用时,对fetchObjects的第二次调用产生与没有它的iPhone上相同的结果.我的问题是:为什么iPad产生的结果与iPhone不同?这是我最近发现和发布的第二个差异.
-(NSError*)deleteObject:(NSManagedObject*)mo;
{
NSLog(@"\n\nNum objects in store before delete: %i\n\n",
[[self.fetchedResultsController fetchedObjects] count]);
[self.managedObjectContext deleteObject:mo];
// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
}
// [self.fetchedResultsController performFetch:&error]; // force a fetch
NSLog(@"\n\nNum objects in store after delete (and save): %i\n\n",
[[self.fetchedResultsController fetchedObjects] count]);
return error;
}
Run Code Online (Sandbox Code Playgroud)
(完整的NSObjectInaccessibleException是:"因未捕获的异常而终止应用程序'NSObjectInaccessibleException',原因:'CoreData无法解决'0x1dcf90的错误
将以下代码添加到FRC委托将解决此问题.
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller { }
Run Code Online (Sandbox Code Playgroud)
感谢BenT在Apple Dev论坛上的回答(参见上面的评论).我向他询问了修复的解释,他说:"iPad正在使用iPhone OS 3.2而iPhone正在使用iPhone.在3.2中对NSFetchedResultsController进行了一些改进,但不幸的是,这对代表们的实际需求产生了副作用实现一个(任何一个)委托方法来获得主动变更跟踪." https://devforums.apple.com/message/221471#221471(希望这有助于某人.常见问题解答说在这种情况下可以回答你自己的问题)