xcode 删除对象 Coredata

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6336071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:15:38  来源:igfitidea点击:

Delete object Coredata

iphoneobjective-cxcodecore-data

提问by James Dunay

I have a project that uses coredata and i am attmepting to delete from what i have stored. But i keep getting this error.

我有一个使用 coredata 的项目,我想从我存储的内容中删除。但我不断收到此错误。

An NSManagedObjectContext cannot delete objects in other contexts.

I looked at what apple had to say and from what i can tell i have it correct, but something is still off. Any suggestions? Thx!

我查看了苹果要说的内容,从我可以说的内容来看,我是正确的,但仍有一些问题。有什么建议?谢谢!

for (UserNumber *info in pinNumberArray) {

        NSSet *time = [[NSSet alloc] initWithSet:info.Times];

        for (ErgTimes *ergTimes in time){

            NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:ergTimes.Twok, nil];
            NSManagedObject *eventToDelete = [temp objectAtIndex:0];
            [managedObjectContext deleteObject:eventToDelete];
        }
    }  

回答by Daniel Thorpe

Well, it's possibly that you've got your objects, context and threads mixed up. NSManagedObjectContextisn't thread safe. To delete an object from a context, you need to have fetched the object "into" the context first, and I guess your managed object was fetched by a different MOC. Without seeing more code I can't tell.

嗯,很可能你把你的对象、上下文和线程搞混了。NSManagedObjectContext不是线程安全的。要从上下文中删除对象,您需要先将对象“提取到”上下文中,我猜您的托管对象是由不同的 MOC 提取的。没有看到更多代码,我无法判断。

However, there is a relatively easy fix. In your for loop, do this instead

但是,有一个相对容易的修复方法。在您的 for 循环中,改为执行此操作

for (ErgTimes *ergTimes in time){
    NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:ergTimes.Twok, nil];
    NSManagedObject *eventToDelete = [managedObjectContext objectWithID:[[temp objectAtIndex:0] objectID]];
    [managedObjectContext deleteObject:eventToDelete];
}

What this does is get the object in the MOC your currently using using its objectID which is thread-safe.

这样做是使用线程安全的 objectID 获取当前使用的 MOC 中的对象。

回答by Massimo Cafaro

You must use the same NSManagedObjectContextyou used to fetch the objects to delete them. Easiest solution: use the managedObjectContext associated to each object to delete it. Like this:

您必须使用与NSManagedObjectContext获取对象相同的方法来删除它们。最简单的解决方案:使用与每个对象关联的 managedObjectContext 来删除它。像这样:

for (UserNumber *info in pinNumberArray) {

    NSSet *time = [[NSSet alloc] initWithSet:info.Times];

    for (ErgTimes *ergTimes in time){

        NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:ergTimes.Twok, nil];
        NSManagedObject *eventToDelete = [temp objectAtIndex:0];
        [eventToDelete.managedObjectContext deleteObject:eventToDelete];
    }
}