Core Data by tutorials 笔记(一)

2018-02-24 15:54 更新

原文出处: http://chengway.in/post/ji-zhu/core-data-by-tutorials-bi-ji

最近花了半个月读完了Raywenderlich家的《Core Data by Tutorials》,接下来几天就做个回顾,笔记并不是对原书的简单翻译,算是对整个知识脉络的一个整理的过程吧:)

Update: 上周去搞Sketch了,工具算是基本入门了,抄了几个图标,画了几个图,以后会再聊聊。好了,进入正题吧,今天打算介绍前三章,也是CoreData的最基础的部分。***

Chapter 1:Your First Core Data App

第一章比较简单,主要是带你熟悉CoreData的用法,并用CoreData创建了一个简单的List APP,学完这章你能加满如下技能点:

  • model data you want to store in Core Data using Xcode’s model editor;
  • add new records to Core Data;
  • fetch a set of records from Core Data;
  • display the fetched results to the user in a table view.

第一章按顺序大致如下:

一、Getting started

作者提供了一个Start Project,打开其实就是一个UITableViewController,你需要做的就是增加一个addName方法,这里使用了UIAlertController这个iOS 8新添加的方法来做数据的添加逻辑,使用起来也很优雅。

二、Modeling your data

这个主要是根据你的数据模型创建对应的managed object model,主要操作也在.xcdatamodeld中进行,全GUI操作,没什么难度。

You can think of a Core Data entity as a class “definition” and the managed object as an instance of that class.
这里作者用了面向对象的思想做了类比,虽然不是很准确,但也方便初学者理解。

三、Saving to Core Data

这里要注意到NSManagedObject的“对象”其实是遵循“KVC”的,NSManagedObject对象的值改变后,在ManagedContext中进行save操作。

四、Fetching from Core Data

创建一个NSFetchRequest对象,设置好各种条件,依旧是交给ManagedContext对象去执行。下面是作者描述了两种fetch失败的情况

If there are no objects that match the fetch request’s criteria, the method returns an optional value containing an empty array.
If an error occurred during the fetch, the method returns an optional value that contains nil. If this happens, you can inspect the NSError and respond appropriately.

第一章到此结束:)


Chapter 2:NSManagedObject Subclasses

NSManagedObject上一章我们提到过,要存取属性,只能用KVC,使用起来既不安全也不符合面向对象的原则,所以我们这章要生成他的子类,来创建我们自己的属性,这样就又能愉快地使用"."语法啦。

第二章的大致顺序如下:

一、Getting started

作者提供了一个“挑选领结”的Start Project,领结数据存放在plist文件中,同样是一个很简单的App。

二、Modeling your data

打开xcdatamodeld文件进行编辑,添加属性。这里主要看一下Binary DataTransformable两种类型:

  • Binary Data这里将image对象保存成了二进制文件,当然任何可以序列化的对象其实都可以保存成二进制。这里作者强调了性能问题,如果将一个很大的二进制保存到SQLite数据库中很可能会产生卡顿等问题。当然,幸运的是Core Data提供了一种叫Allows External Storage的解决方式,他只对binary data的属性类型有效,你只需要简单的开启他就好了。

    When you enable Allows External Storage, Core Data heuristically decides on a per-value basis if it should save the data directly in the database or store a URI that points to a separate file.

  • Transformable除了一些基本属性,如果一个对象遵循NSCoding Protocol,那么这个对象是可以选择使用transformable类型的,作者这里使用的是UIColor类型,遵循NSSecureCoding协议,而该协议又继承自NSCoding,所以设为Transformable是OK的。自定义的类想要设置为Transformable,那么你首先要实现NSCoding protocol

三、Managed object subclasses

在Xcode中选择Editor\Create NSManagedObject Subclass创建managedObject对象的子类,这里注意下Map Model中的attribute type与实际子类对象中的属性的对应关系就好了

  • String maps to String
  • Integer 16/32/64, Float, Double and Boolean map to NSNumber
  • Decimal maps to NSDecimalNumber
  • Date maps to NSDate
  • Binary data maps to NSData
  • Transformable maps to AnyObject

当然如果你想保留Map Model中的原始基本类型,那么在创建NSManagedObject Subclass时要勾选Use scalar properties for primitive data types

Similar to @dynamic in Objective-C, the @NSManaged attribute informs the Swift compiler that the backing store and implementation of a property will be provided at runtime instead of at compile time.
这里注意下两种语言的一点区别

创建完子类还要记得一点就是在Model中的Attributes inspectorclass name设为你新生成的子类路径,完成这一步主要是为了在runtime时将managed object subclass与Model中的entity链接起来。(有点类似与在SB中创建一个VC,并设置他的custom class)。

四、Propagating a managed context

因为Xcode默认的CoreData模板会将context在AppDelegate中创建,所以,就会有这种获取context的做法:

 let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate 

但这种做法还是不推荐使用,较好的方法是能够在对象的的初始化中将context作为参数进行传递。
接下来,作者从plist中将数据通过子类对象导入到core data中,再对界面做了些操作,这都比较简单。

五、Data validation in Core Data

数据校验这里可以在data model inspector 中设置最大最小值,如果出错会在context save的时候将错误信息传给error参数

第二章到此结束:)


Chapter 3:The Core Data Stack

Core Data要正常运行,需要几个组件共同协作来完成,这些组件包括:

  • NSManagedObjectModel
  • NSPersistentStore
  • NSPersistentStoreCoordinator
  • NSManagedObjectContext

这些组件共同构成了Core Data Stack,组件间的关系我觉得用苹果官方提供的一张图来展示最好不过了

以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号