[NSManagedObjectContext delete:]: 无法识别的方法发送给实例

4

Core Data让我很烦恼。我正在删除旧数据并希望插入从服务器接收到的新数据。

现在删除部分出现了“sigabort”:

-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0x522f550 2013-09-27 14:05:56.592 * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext delete:]: unrecognized selector sent to instance 0x522f550' * First throw call stack: (0x320f82a3 0x39d4797f 0x320fbe07 0x320fa531 0x32051f68 0x1b6c53 0x1868e5 0x3a15f11f 0x3a16d259 0x3a16d3b9 0x3a193a11 0x3a1938a4) libc++abi.dylib: terminate called throwing an exception

删除操作在后台进行,nsmanagedobjectcontext是私有并发类型。我打印了上下文和对象上下文的指针。

po context NSManagedObjectContext: 0x522f550

以及

po tmpCon.managedObjectContext NSManagedObjectContext: 0x522f550>

删除的代码如下:

     NSError *errorAllCons = nil;
    NSFetchRequest *allevents = [[NSFetchRequest alloc] init];
    [allevents setEntity:[NSEntityDescription entityForName:@"TEventContact" inManagedObjectContext:context]];
    NSArray *allCons = [context executeFetchRequest:allevents error:&errorAllCons];
    for (TEventContact *tmpCon in allCons)
    {
        [context delete:tmpCon];
    }

我的代码在 [context delete:tmpCon]; 处崩溃了,有没有人能告诉我错在哪里?


1
NSManagedObjectContext 没有 delete: 方法,也许是 deleteObject: - pNre
2个回答

24

使用

 [context deleteObject:tmpCon];

它将解决这个问题


2
这是永久删除对象的方法:

按照以下方式执行

for (TEventContact *tmpCon in allCons)
{
    [context deleteObject:tmpCon] /// for deleting object in context
}    
[context save]; /// to reflect changes in database you need to save that context

如果不保存上下文,它就不会反映在数据库中,这可能会导致另一个上下文的脏读取。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接