coredata栈

上下文包含所有信息

NSManagedObjectModel

The NSManagedObjectModel instance describes the data that is going to be accessed by the Core Data stack. During the creation of the Core Data stack, the NSManagedObjectModel is loaded into memory as the first step in the creation of the stack. The example code above resolves an NSURL from the main application bundle using a known filename (in this example DataModel.momd) for the NSManagedObjectModel. After the NSManagedObjectModel object is initialized, the NSPersistentStoreCoordinator object is constructed.

NSPersistentStoreCoordinator

The NSPersistentStoreCoordinator sits in the middle of the Core Data stack. The coordinator is responsible for realizing instances of entities that are defined inside of the model. It creates new instances of the entities in the model, and it retrieves existing instances from a persistent store (NSPersistentStore). The persistent store can be on disk or in memory. Depending on the structure of the application, it is possible, although uncommon, to have more than one persistent store being coordinated by the NSPersistentStoreCoordinator.

Whereas the NSManagedObjectModel defines the structure of the data, the NSPersistentStoreCoordinatorrealizes objects from the data in the persistent store and passes those objects off to the requesting NSManagedObjectContext. The NSPersistentStoreCoordinator also verifies that the data is in a consistent state that matches the definitions in the NSManagedObjectModel.

The call to add the NSPersistentStore to the NSPersistentStoreCoordinator is performed asynchronously. A few situations can cause this call to block the calling thread (for example, integration with iCloud and Migrations). Therefore, it is better to execute this call asynchronously to avoid blocking the user interface queue.

NSManagedObjectContext

The managed object context (NSManagedObjectContext) is the object that your application will interact with the most, and therefore it is the one that is exposed to the rest of your application. Think of the managed object context as an intelligent scratch pad. When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects however you like. Unless you actually save those changes, however, the persistent store remains unaltered.

All managed objects must be registered with a managed object context. You use the context to add objects to the object graph and remove objects from the object graph. The context tracks the changes you make, both to individual objects’ attributes and to the relationships between objects. By tracking changes, the context is able to provide undo and redo support for you. It also ensures that if you change relationships between objects, the integrity of the object graph is maintained.

If you choose to save the changes you have made, the context ensures that your objects are in a valid state. If they are, the changes are written to the persistent store (or stores), new records are added for objects you created, and records are removed for objects you deleted.

Without Core Data, you have to write methods to support archiving and unarchiving of data, to keep track of model objects, and to interact with an undo manager to support undo. In the Core Data framework, most of this functionality is provided for you automatically, primarily through the managed object context.

时间: 2024-08-09 22:02:42

coredata栈的相关文章

iOS中文版资源库,非常全

目录 入门 库和框架 音频 动画 Apple TV 桥接 缓存 Core Data 图表 数据库 硬件 动作 蓝牙 位置 iBeacon HUD 事件总线( EventBus ) 文件 JSON 布局 日志 地图 媒体 图片 视频 PDF 消息 网络 推送通知 Passbook 权限 文本 浏览 / 介绍 / 教程 URL Scheme UI Websocket 代码质量 分析 支付 产品化工具 实用工具 安全 安装项目 依赖 / 包管理 测试 测试驱动开发(TDD) / 行为驱动开发(BDD)

iOS中文版资源库

我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列的资源整理.awesome-ios 就是 vsouza 发起维护的 iOS 资源列表,内容包括:框架.组件.测试.Apple Store.SDK.XCode.网站.书籍等.Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的项目则会被标记为 ▲. Awesome 系列虽然挺全,但基本只对收录的资源做了极为简要的介绍,如果有更详细的中文介绍,对相应开发者的帮助会更大.这也是我们发起这个开源项目的初衷.

iOS-资源大全

本文由 伯乐在线 - ARIGATO 翻译,黄利民 校稿.未经许可,禁止转载!英文出处:vsouza.欢迎加入翻译组. 这是个精心编排的列表,它包含了优秀的 iOS 框架.库.教程.XCode 插件.组件等等. 这个列表分为以下几个部分:框架( Frameworks ).组件( Components ).测试( Testing )和其他的开源项目,免费的和付费的服务.每个部分中的项目没有先后顺序,均是按照提交的先后顺序排列的. Swift 语言写成的项目会被标记为 ★ ,AppleWatch 的

七方件业金离以提领前群约会ODjdidtlwfWv

为了从不同环节,尤其与广大使用人群直接关系的环节反映质量状况,对共享自行车投放点.运营仓库.生产企业等不同环节的产品抽查,覆盖了共享自行车从成品出厂到待投放的关键环节. 该负责人称,根据新车投放情况,结合共享自行车行业市场占有分布特点,本次重点抽查了摩拜.ofo.Hellobike三个品牌的产品,占本次抽查批次总数的83.3%.其中,在天津.无锡.武汉.广州.深圳.东莞6个城市抽查了9批次摩拜产品,占产品抽查批次总数的37.5%,抽查批次合格率88.9%,抽查不合格的1批次产品为待投放于广州市的

iOS开发——数据持久化OC篇&(七)CoreData高级常识

CoreData高级常识 关于CoreData貌似实际开发中很少用到,基本上是个有九个公司不会使用它,因为都说是性能不好,但是作为一个程序员,了解及其使用时必须了, 下面是我从一位大神那里搬过来的一下Core详细介绍,相信以后总有一天会帮我解决不少学习CoreData中的问题! 一.技术概览 1. Core Data 功能初窥 对于处理诸如对象生命周期管理.对象图管理等日常任务,Core Data框架提供了广泛且自动化的解决方案.它有以下特性. (注:对象图-Object graph的解释:在面

iOS-数据持久化-CoreData

CoreData详解 介绍: 在Cocoa环境下,如果你想使用数据库(如sqlite),你可以使用sql语句的方式通过相关的工具类进行数据库的直接操作.当然你也可以通过别人封装之后的一些简单框架,使得你的操作更加简单(如FMDB BNRPersistence). Cocoa框架本身提供了CoreData这个API可方便的让开发者通过操作对象的方式在操作数据库.CoreData是一个对象图(object graph)以及持久化的管理框架.我们可以通过CoreData创对象,设置好象之间的关系,然后

CoreData学习-最好的一片文章

CoreData学习-最好的一片文章 分类: IOS重新上路2014-05-25 18:00 1937人阅读 评论(0) 收藏 举报 目录(?)[+] 写的很好的一篇教程,我什么时候能写出这么棒的文章来,还是继续努力学习吧. iOS教程:Core Data数据持久性存储基础教程 其实最近更多的是在写这篇文章<iOS教程:使用持久性数据Core Data>,这篇是<iOS开发教程:Storyboard全解析-第一部分>这篇的后续,但是目前还没有完成,先放出一个持久性数据存储的教程以供

堆和栈的区别 -- 本次存贮哪几种方式

栈区(stack) --由编译器自动分配释放,存放函数的参数值,局部变量的值. 堆区 (heap) ---一般有程序员分配释放. 全局区(静态区)(static) --全局变量和静态变量,程序结束后由系统释放. 文字常量区 -- 常量字符串存放在这里,程序结束后由系统释放. 程序代码区 ---存在行数体的二进制文件. 栈:只要栈的剩余空间大于所申请空间,系统将为程序提供内存,否则将报异常提示栈溢出. 堆:首先应该知道操作系统有一个记录空闲内存地址的链表,当系统收到程序的申请时,会遍历该链表,寻找

iOS-Senior-数据处理之CoreData

1.CoreData数据库框架的优势 CoreData数据持久化框架是Cocoa API的一部分,首次在iOS5版本的系统中出现,它允许按照实体 - 属性 - 值 模型组织数据,并以XML,二进制文件或者SQLite数据文件的格式持久化数据. CoreData主要提供对象 - 关系映射(ORM)功能,把oc对象转化为数据保存到文件,也可以数据转化为oc对象. sqlite: 1.基于c接口,需要使用sql语句,代码繁琐 2.在处理大量数据时,表关系更直观 3.在oc中不是可视化的 CoreDat