托管对象未在我的核心数据库中正确删除 [英] Managed object not deleted properly in my core database
问题描述
我正在尝试从我的实体预订
中删除一些对象。我这样做。
I am trying to delete some objects from my Entity Reservation
. I am doing it like this.
NSManagedObjectContext *context = reservation.managedObjectContext;
[context deleteObject:reservation];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Error is %@",error);
}
之后,我删除了我的对象,获取整个实体
。我可以看到对象是从我的实体
删除。但是当我重新启动我的应用程序,我在上一次会话中删除的所有对象都存储在我的实体
。
After that I delete my object I fetch the whole entity
again. I can see that the object is delete from my entity
. But when I restart my app, all my objects that I deleted in the previous session are back stored in my entity
.
我使用 restkit
存储我从web服务返回的对象。此外,当我删除对象,我删除它也在我的数据库。
I'm using restkit
to store my objects that I got back from a webservice. Also when I delete the object, I delete it also in my database.
当我重新启动我的应用程序,看看我的日志,我看到我没有从我的Web服务,我在上一个会话中删除的对象,所以这是oké 。唯一的问题是它们以某种方式存储在我的核心数据库中。
When I restart my app and look at my logs I see that I don't get the object back from my webservice that I deleted in the previous session, so that's oké. The only problem is that they are somehow stored back in my core database
.
推荐答案
我不完全确定reskit如何处理线程上的保存/删除,但基本上当您在注册通知的主线程上设置NSManagedObjectContext。
I am not entirely sure how reskit handles saving/deleting on threads but basically when you setup your NSManagedObjectContext on the main thread you register for notifications.
-(NSManagedObjectContext *)aContext
{
if (!_aContext) {
_aContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(mergeChanges:) name:NSManagedObjectContextDidSaveNotification object:nil];
}
return _aContext;
}
如果在后台线程上发生对上下文的更改, 。
If a change to context happens on a background thread then a notification will be sent.
-(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];
});
}
这篇关于托管对象未在我的核心数据库中正确删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!