Object-Relational Mapper, but rather an object graph and persistence framework, capable of much more than the Active Record pattern alone is capable of. Using Core Data as an ORM necessarily limits the capabilities of Core Data and muddies its conceptual purity. But for many developers longing for the familiarity of an ORM, this trade-off is a deal at twice the price! » Mattt Thompson, @mattt, http://nshipster.com/core-data-libraries-and-utilities/ What is Core Data?
returns Record (Pure Value elements) from a sets of tables. •Using a graph involves looking for object instances (node) from a specific graph instance. Graph !!= ORM
look at the documentation (concurrencyType will be explained in a next workshop): // Swift let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType) // Objective-C NSManagedObjectContext * ctx = [[NSManagedObjectContext alloc] initWithConcurrencyType: NSMainQueueConcurrencyType]; Using Code: Context initialisers
complex graph that have different kind of nodes (aka. nodes with different properties) and that have interactions. Example: Boss #1 name: Javier Employee #2 name: Damien speciality: iOS Boss #2 name: Mathieu Boss #3 name: Samy Employee #2 name: Alberto speciality: iOS Employee #2 name: Elaine speciality: test Employee #2 name: Khali speciality: test underBosses(*) employees (*) boss (1) bosses (*)
•KVC compliant (KVC programming guide -> Compliance Checklist) •@NSManaged looks like magic but it is not. Create subclasses! Playground - Editing the node - Subclasses @objc(Boss) public class Boss: NSManagedObject { @NSManaged var name: String? @NSManaged var bosses: Set<Boss>? @NSManaged var employees: Set<Employee> @NSManaged func addEmployeesObject(_ employee: Employee) } @objc(Employee) public class Employee: NSManagedObject { @NSManaged var name: String? @NSManaged var boss: Boss? }
generation to support the Objective-C 2 properties feature (see Declared Properties) by automatically creating a subclass of the class appropriate for entity. initWithEntity:insertIntoManagedObjectContext: therefore returns an instance of the appropriate class for entity. The dynamically-generated subclass will be based on the class specified by the entity, so specifying a custom class in your model will supersede the class passed to alloc. NSManagedObject documentation
transformable attribute and one NSValueTransformer class that will marshal/unmarshall your class into a CoreData primitive type. Custom properties If no transformer is specified, the value transformer specified NSKeyedUnarchiveFromDataTransformerName is used (See Core Data Release Notes). In this case y ou only need to make your custom type conformed to NSCoding
with two flavours: •NSAtomicStore: each write implies re-written all the data from all the graphs. •NSIncrementalStore: each write implies writing only the changes. NSPersistentStore Atomic Incremental iOS Binary In-Memory, SQLite macOS Binary, XML In-Memory, SQLite
too complex for now. •NSAtomicStore: -> See Atomic Store Fundamentals •NSIncrementalStore: -> See Incremental Store Programming Guide and WWDC 201 1 Session 303 NSPersistentStore too deep for now
model and validate the inputs before persisting? How to deal/synchronise different graph instances (aka MOC)? How to deal with different data location (aka different NSPersistentStore)? Consistency of data
and consistency according to the model provided at initialisation •Transmits changes from MOC to the stores once MOC called save() •Can Handle multiples NSPersistentStore instances NSPersistanceStoreCoordinator Playground - Store and Coordinators
object. Request can be executed either on the context or on the store coordinator •entityName -> Which kind of object to return •predicate -> Only includes object matching the predicate •fetchLimit -> Limit the number of object to fetch •fetchOffset -> Skip object in the returned set •fetchBatchSize -> Lazy load the object in batch •sortDescriptors -> Sort the set according to this descriptors Identifying data Playground - Basic requests
elements and with a great depth, you probably do not want to load all the graph. By default, all children of a returned NSManagedObject are returned as Fault (lazy loaded object) CoreData will only load those children once you access to it. When you fetch in batch, the same principle is used. Faulting Playground - Faulting
Release Notes Predicate Programming Guide NSExpression documentation WWDC 2010 128 - Mastering Core Data Core Data Second Edition - Marcus Zara Core Data - Florian Kugler/Daniel Eggert objc.io Issue 4 - Core Data https://www.objc.io/issues/4-core-data/