我正在尝试从Entity Reservation
中删除一些对象。我是这样做的。
NSManagedObjectContext *context = reservation.managedObjectContext;
[context deleteObject:reservation];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error is %@",error);
}
之后我删除了我的对象,我再次获取整个entity
。我可以看到该对象已从我的entity
中删除。但是当我重新启动应用程序时,我在上一个会话中删除的所有对象都会存储在我的entity
中。
我正在使用restkit
存储我从Web服务中获取的对象。此外,当我删除对象时,我也在我的数据库中删除它。
当我重新启动我的应用程序并查看我的日志时,我发现我没有从我在上一个会话中删除的webservice中获取该对象,所以没关系。唯一的问题是它们以某种方式存储在我的core database
。
答案 0 :(得分:0)
我不完全确定reskit如何处理线程上的保存/删除,但基本上当您在注册通知的主线程上设置NSManagedObjectContext时。
-(NSManagedObjectContext *)aContext
{
if (!_aContext) {
_aContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:nil];
}
return _aContext;
}
如果在后台线程上发生了对上下文的更改,则会发送通知。
-(void)mergeChanges:(NSNotification *)aNotification
{
// Merge the changes from the notification
// on the main thread as this is the main
// context for our application.
dispatch_async(dispatch_get_main_queue(), ^{
[self.aContext mergeChangesFromContextDidSaveNotification:aNotification];
});
}