【问题标题】:Core Data after deletion throwing an Error while saving删除后的核心数据在保存时抛出错误
【发布时间】:2012-07-27 12:13:00
【问题描述】:

删除后保存时抛出以下错误。 NSManagedObjectContext 不能删除其他上下文中的对象。

我还检查了从中获取数据的 managedobjectcontext 是否与删除数据的 managedobjectcontext 相同。结果他们俩是平等的。你可以看到下面的比较。

    NSManagedObjectContext *managedobjectcontext=[Singleton managedObjectContext];    

    NSArray *allprebuyers=[Fetchsavefromcoredata arrayfromentityresult:@"Buyer"];


    for(int i=0;i<[allprebuyers count];i++)
    {
        Buyer *buyerobj=[allprebuyers objectAtIndex:i];

        NSLog(@"class name : %@",NSStringFromClass([buyerobj class]));

        //object comparison for fetched moc and moc which is deleting, log says Equal.
        if ([[buyerobj managedObjectContext] isEqual:managedobjectcontext]) 
        {
            NSLog(@"Equal");
        }
        else 
        {
            NSLog(@"Not Equal");
        }
        [managedobjectcontext deleteObject:buyerobj];

        NSError *error=nil;

        [managedobjectcontext save:&error];

    }

我一直在努力解决这个问题,任何帮助将不胜感激。

【问题讨论】:

  • 这行得通吗? [[buyerobj] managedobjectcontext] deleteObject:buyerobj];

标签: iphone ios core-data nsmanagedobject nsmanagedobjectcontext


【解决方案1】:

您必须选择删除托管对象。

1) 在拥有的上下文中

[object.managedObjectContext deleteObject: object];

2) 在另一种情况下

[anotherContext deleteObject: [anotherContext objectRegisteredForID: object.objectID]];

如果对象在另一个上下文中注册,则不能删除该上下文中的对象。

另外,请注意上下文与线程或队列(主队列或私有队列)相关联。确保从正确的线程/队列访问上下文。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    相关资源
    最近更新 更多