核心数据deleteObject:不工作? [英] Core Data deleteObject: not working?

查看:116
本文介绍了核心数据deleteObject:不工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

+(void)deleteObject:(NSManagedObjectID*)oId {
NSError *error;
DFAppDelegate *temp = [DFAppDelegate new];
NSManagedObjectContext *context = [temp managedObjectContext];
NSManagedObject *obj = [context existingObjectWithID:oId error:&error];
[context deleteObject:obj];
}

但它似乎无法正常工作。当我在iOS模拟器上重新启动我的应用程序时,我可以在列表中再次看到对象。
我试图打印具有给定对象ID的对象,并且它返回正确的对象,但仍然没有从我的核心数据模型永久删除对象。
我的实体与其他实体无关。

But it doesn't seem to work accordingly. When I restart my application on iOS simulator I can see the object again in the list. I've tried to print the object with the given object id and it is returning the correct object but still the object is not deleted permanently form my core data model. None of my entity is in relationship with another entity.

任何人都可以向我解释发生了什么问题?

Can anyone explain me what's going wrong?

谢谢。

编辑:
我检查错误,但没有显示错误。

I've checked for error, but it shows no error.

推荐答案

NSManagedObjectContext 所做的任何更改都是临时的,直到您保存为止。尝试将此添加到方法的结尾:

Any changes that you make to a NSManagedObjectContext are temporary until you save it. Try adding this to the end of your method:

if (![context save:&error]) {
     NSLog(@"Couldn't save: %@", error);
}

这篇关于核心数据deleteObject:不工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆