做项目的时候在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表示,这个概念类似于单例。

图2    Independent faults for a department object

图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的更多相关文章

  1. Core Data 数据出现Fault

    I am mapping Json Data from Server using Restkit and I am Displaying those data by fetching from db. ...

  2. 《驾驭Core Data》 第二章 Core Data入门

    本文由海水的味道编译整理,请勿转载,请勿用于商业用途.    当前版本号:0.4.0 第二章 Core Data入门 本章将讲解Core Data框架中涉及的基本概念,以及一个简单的Core Data ...

  3. Core Data 学习简单整理01

    Core Data是苹果针对Mac和iOS平台开发的一个框架, 通过CoreData可以在本地生成数据库sqlite,提供了ORM的功能,将对象和数据模型相互转换 . 通过Core Data管理和操作 ...

  4. iOS之Core Data及其线程安全

    一.简介 Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对 ...

  5. Core Data的一些常见用法

    一.简介 Core Data是一个纯粹的面向对象框架,其本质就是一个ORM(对象关系映射:Object Relational Mapping),能以面向对象的方式操作SQLite数据库.在实际开发中绝 ...

  6. Core Data 使用映射模型

    Core Data 使用映射模型 如果新版本的模型存在较复杂的更改,可以创建一个映射模型,通过该模型指定源模型如何映射到目标模型. 创建映射模型,新建File,  Core Data 选择Mappin ...

  7. SELF, self in CORE DATA

    Predicate SELF Represents the object being evaluated. CORE DATA Retrieving Specific Objects If your ...

  8. Core Data浅谈初级入门

    Core Data是iOS5之后才出现的一个框架,它提供了对象-关系映射(ORM)的功能,即能够将OC对象转化成数据,保存在SQLite数据库文件中,也能够将保存在数据库中的数据还原成OC对象.在此数 ...

  9. Core Data

    •   Core Data   是 iOS SDK   里的一个很强大的框架,允许程序员 以面向对象 的方式储存和管理数据 .使用 Core Data 框架,程序员可以很轻松有效 地通过面向对象的接口 ...

随机推荐

  1. C/C++ fgets

    {     str_normalize_init();    unsigned options = SNO_TO_LOWER | SNO_TO_HALF;    if (argc > 1)    ...

  2. 强制位(set uid和set gid)与冒险位(sticky)的作用都有什么

    1.set uid(强制位) 只能对文件进行设置 通过对文件设置uid可以使非文件拥有者或文件所属群组的用户具有执行该文件的权限. 例如ping,默认情况下所有用户都可以使用此命令,但当我们查看pin ...

  3. js-统计中文,英文,字符的个数

    function getByteLen(val) { ; ; i < val.length; i++) { var a = val.charAt(i); if (a.match(/[^\x00- ...

  4. shutil库文件的操作

    一.拷贝,移动,改名 import shutil old_file=r"C:\Users\ffm11\Desktop\AI.docx" new_file=r"C:\Use ...

  5. C#调用Mail发送QQ邮件

    需要用到: 1.System.Net.Mail; 2.QQ邮箱的POP3/SMTP服务码 QQ邮箱的POP3/SMTP服务码获取方法: 1.打开qq邮箱: 2.进入设置页面-->账户:(往下翻) ...

  6. 4.2 react patterns(转)

    修改 Props Immutable data representation 确定性 在 getInitialState 中使用 props 私有状态和全局事件 render 包含 side effe ...

  7. source insight和vim同时使用

    https://blog.csdn.net/wangn222/article/details/72721993 1.Source Insight中,Options->Custom Command ...

  8. ajax实现异步刷新

    1. 导入 json 包: jackson-annotations-2.8.9.jar jackson-core-2.8.9.jar jackson-databind-2.8.9.jar json.j ...

  9. Redis数据结构之字典-dict

    dict是Redis服务器中出现最为频繁的复合型数据结构,除hash使用dict之外,整个Redis数据库中所有的key和value也会组成一个全局字典,还有带过期时间的key集合也是一个字典. zs ...

  10. 第五篇 scrapy安装及目录结构,启动spider项目

    实际上安装scrapy框架时,需要安装很多依赖包,因此建议用pip安装,这里我就直接使用pycharm的安装功能直接搜索scrapy安装好了. 然后进入虚拟环境创建一个scrapy工程: (third ...