首页 > 代码库 > Chapter 23 Core Data

Chapter 23 Core Data

Chapter 23 Core Data

 

1.  Core Data is only able to store certain data types in its store, and UIImage is not one of these types. Instead, you declared the UIImage as transformable. With a transformable attribute, Core Data will convert the object into NSData when saving, and convert the NSData back into the original object when loading it from the file system. In order for Core Data to do this, you have to supply it with an NSValueTransformer subclass that handles these conversions. 

 

2. An NSManagedObject is little more than a data container. If you need your model objects to do something in addition to holding data, you must subclass NSManagedObject. Then, in your model file, you specify that this entity is represented by instances of your subclass, not the standard NSManagedObject.

 

3. The portal through which you talk to the database is the NSManagedObjectContext. The NSManagedObjectContext uses an NSPersistentStoreCoordinator. You ask the persistent store coordinator to open a SQLite database at a particular filename. The persistent store coordinator uses the model file in the form of an instance of NSManagedObjectModel. The persistent store coordinator needs to know two things:” What are all of my entities and their attributes and relationships?” and “Where am I saving and loading data from?” To answer these questions, you need to create an instance of NSManagedObjectModel to hold the entity information and initialize the persistent store coordinator with this object. Then you will create the instance of NSManagedObjectContext and specify that is use this persistent store coordinator to save and load objects.

 

4.  To get objects back from the NSManagedObjectContext, you must prepare and execute an NSFetchRequest. After a fetch request is executed, you will get an array of all the objects that match the parameters of that request.

Chapter 23 Core Data