删除托管对象后核心数据故障 [英] Core Data fault after deleting managed object

查看:78
本文介绍了删除托管对象后核心数据故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我正在尝试从 NSManagedObjectContext 中删除​​a的一些属性并保存上下文。问题在于,保存上下文后,Core Data将对象数据标记为错误,并且显然无法恢复。

Basically, I'm trying to access some properties of a after deleting it from the NSManagedObjectContext and saving the context. The problem is that, after saving the context, Core Data marks the object data as fault and, apparently, is unable to recover it.

我创建了一个示例项目为了重现该问题,您可以在此处下载它。为了说明,下面的代码段:

I've created a sample project in order to replicate the issue, you can download it here. To illustrate, the following snippet:

City *city = [self.cities objectAtIndex:indexPath.row];
[self.managedObjectContext deleteObject:city];
if (![self.managedObjectContext save:&error]) {
    [self.managedObjectContext rollback];
    NSLog(@"Error: %@", error);
}else{
    NSLog(@"%@", city);
    // All properties of "city" are zeroed.
    // Saved. Update data sources and animate changes...
}

产生:

<City: 0x7fe1cbd3cba0> (entity: City; id: 0xd000000000040004 <x-coredata://C1E3D3D8-188D-41DE-B701-08AF6D3E8860/City/p1> ; data: {
    country = "0xd000000000080002 <x-coredata://C1E3D3D8-188D-41DE-B701-08AF6D3E8860/Country/p2>";
    name = Rosario;
})

<City: 0x7fe1cbd3cba0> (entity: City; id: 0xd000000000040004 <x-coredata://C1E3D3D8-188D-41DE-B701-08AF6D3E8860/City/p1> ; data: <fault>)

删除托管对象后,我想访问它的原因是更新 NSMutableArray 用作表视图的数据源,并更新导航控制器堆栈中前一个控制器中的另一个数据源(在示例项目中未实现)。

The reason I'd like to access the managed object, after deleting it, is to update a NSMutableArray which acts as data source for a table view and update another data source in a previous controller in the navigation controller stack (This is not implemented in the sample project).

总结一下,我的问题是:

Wrapping up, my questions are:

从其 NSManagedObjectContext删除 NSManagedObject 之后, code>并保存上下文,是否不再保证可以访问托管对象中的数据?即使保留了对该托管对象的引用?

After deleting a NSManagedObject from its NSManagedObjectContext and saving the context, it is no longer guaranteed that the data in the managed object will be accessible? Even if a reference to that managed object is kept?

根据我的研究,Core Data摆脱了实体数据以在上下文被保存后节省内存已保存。这个假设正确吗?

Based on what i've researched, Core Data is getting rid of the entity data to save memory once the context is saved. Is this assumption correct? Are there other factors that might be causing this data faulting?

谢谢。

推荐答案

总是动态呈现 NSManagedObject 。因此,如果将其删除,Core Data会将数据故障。它不存在了。您真正的问题是如何从各种数组中删除对象?首先,在使用任何搜索技术删除对象之前,应先将其删除。这是最简单,最可靠的途径。其次,对象指针本身仍然有效,并且可以与 -removeObject:调用一起使用。让我强调一下,这是一个脆弱的解决方案。我强烈建议您先删除对象,然后再删除它。

An NSManagedObject is always dynamically rendered. Hence, if it is deleted, Core Data faults out the data. It doesn't exist anymore. Your real question is how to remove an object from your various arrays? First, you should remove it before you delete the object using whichever search techniques you wish. This is the easiest and most robust path. Second, the object pointer itself is still valid and can be used with a -removeObject: call. Allow me to emphasize though, this is a fragile solution. I'm strongly encouraging you to remove the object before deleting it.

在回答第二个问题时,


还有其他因素可能导致此数据错误吗?

Are there other factors that might be causing this data faulting?

否。删除对象会导致故障。如果到目前为止数据是可用的,那是由于它是一种实现特征。编写实现而不是规范,尤其是使用数据库技术时,充满了各种生命周期问题。引用明智的医生,不要那样做。

No. Deleting the object is causing the faulting. If the data has heretofore been available, that is due to it being an implementation characteristic. Writing to the implementation instead of the spec, especially with database technologies, is fraught with all sorts of life cycle problems. Quoting the wise Doctor, "Don't do that."

这篇关于删除托管对象后核心数据故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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