Core Data could not fulfill a fault

做项目的时候在iOS4系统遇到过这样一个crash,console显示的错误信息是“Core Data could not fulfill a fault”。

字面意思是什么?“Core Data 无法完成一个错误”,直觉上认为这样翻译肯定是不对的,fault怎么可以fulfill。百思不得其解,唯有求助google,在官方文档《Core Data Programming Guide》(https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData)的“Troubleshooting Core Data”一章中提到:

Fault cannot be fulfilled

Problem: You see the error message,"Core Data could not fulfill a fault".

Cause: The corresponding object'sunderlying data has been deleted from the persistent store.

Remedy: You should discard this object.

相应对象的基础数据已被从持久存储中删除,你应该抛弃这个对象。

问题成因有两种:

其一:

·       存在对某个managed object的强引用;

·       从managed objectcontext中删除这个managed object;

·       保存managed objectcontext的变化;

At this point, the deleted object has been turned into a fault. It isn’t destroyed because doing sowould violate the rules of memory management.

·       尝试从之前retain的引用中取一个attribute或relationship。

Core Data will try to fault the faultedmanaged object but will fail to do so because the object has beendeleted from the store. That is, there is no longer an object with the sameglobal ID in the store.

这种情况类似于使用一个已释放对象的成员变量或者调用其方法,即常见的野指针问题。

其二:

·       从一个managed object context中删除某个managed object;

·       未能打破从其他objects到该object的relationships;

·       保存变化。

At this point, if you try to fire the relationship from someother object to that object, it may fail (this depends on the details of theconfiguration of the relationship as that affects how the relationship isstored).

这种情况类似于对象A的delegate对应的对象B释放时,没有将A的delegate设为nil,即没有解除代理关系,其后A继续使用其delegate的方法。这里要强调的是relationship的方向性,其他不再赘述。

通过以上分析以及程序调试,可以确定是第一个原因,这时bug已经能够解决了,至于为什么只在iOS4系统会出现不得而知。但是对fault还是没能弄明白,继续查找,发现文档有这个章节“Faulting and Uniquing”,看完以后,豁然开朗!

 

Faulting is a mechanism Core Dataemploys to reduce your application’s memory usage. A relatedfeature called uniquing ensures that, in a given managed object context, younever have more than one managed object to represent a given record.

 

Faulting

Faulting是Core Data用来减少应用的内存用量的机制。Faulting限制了object graph的大小。A fault is a placeholder object that represents a managed objectthat has not yet been fully realized, or a collection object that represents arelationship。

图1    A department represented by a fault 

上图很好的诠释了faulting。Employee的manager、department和reports都是一个fault对象,department的持久变量name、budget和employees却并未初始化。如果持久变量比如name被访问,Core Data会自动初始化这个fault对象,并将数据读取到内存中,这个过程被称作firingthe fault,对用户是透明的。

Turning a managed object intoa fault则是相反过程:能够释放多余的内存,将object的in-memory属性设为nil,并且解除对相关objects的强引用。转换过程可以使用refreshObject:mergeChanges:方法来完成。

 

Uniquing

Uniquing使得,在一个特定的managed object context中,一条记录只能由一个managed object表示,这个概念类似于单例。

 Core Data could not fulfill a fault_第1张图片

图2    Independent faults for a department object

Core Data could not fulfill a fault_第2张图片 

图3    Uniqued fault for two employees working inthe same department

 

从图2和图3可以看出,如果不使用uniquing,那么如果要获取所有雇员的department信息,就会fire同样数目的faults,每次都会创建一个新的department对象。这样不仅会占用数倍内存,更会造成数据混乱而无法正确保存context。

总结,faulting和uniquing都是控制内存使用的有效手段。

你可能感兴趣的:(Core Data could not fulfill a fault)