Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Core Data

Rob Brown
February 10, 2011
130

Core Data

Core Data can be intimidating at first. With a proper introduction, the transition into using Core Data can be much smoother. This presentation gives an overview of the core and advanced parts of Core Data.

Rob Brown

February 10, 2011
Tweet

Transcript

  1. Core Data Layers XML SQLite Binary In Memory Custom NSPersistentStore

    NSPersistentStoreCoordinator NSManagedObjectModel NSManagedObjectContext NSManagedObject Application NSManagedObject Subclass
  2. Persistent Store NSPersistentStore Four storage types: XML (Mac only), SQLite,

    binary, and in memory Developers can add their own custom formats
  3. Persistent Store Coordinator NSPersistentStoreCoordinator Façade between persistent store(s) and managed

    object context(s) May only have one MOM Not thread safe! lock, unlock, and tryLock methods are provided.
  4. Managed Object Model NSManagedObjectModel A representation of the app’s data

    model Visually built in Xcode (usually) Commonly called a “MOM”
  5. Entities NSEntityDescription Entities represent objects Entities can have attributes such

    as numbers and strings Entities can have to-one and to-many relationships to other entities
  6. Managed Object Context NSManagedObjectContext The “scratch pad” of you application

    Changes aren’t saved until calling save: Always check for errors Create a new context for each thread Commonly called a “MOC”
  7. Managed Object NSManagedObject Superclass of all managed objects Properties can

    be accessed via KVC/KVO Don’t pass managed objects between threads, pass their objectID instead
  8. Fetch Request Efficiency Smaller batches may be requested for greater

    efficiency (SQLite only) Objects may be returned as faults SUBQUERY can be used to have the database do the work instead of Core Data Jedi Level
  9. Fetch Request Templates Common fetch requests can be stored as

    a template For example, all persons with a given name Use fetchRequestFromTemplateWithName: substitutionVariables: Jedi Level
  10. Transient Attributes Attributes in the local object but not in

    the store Typically used for convenience Example: fullName = “firstName lastName” Requires adding code to generated objects Jedi Level
  11. Delete Rules Deny: Don’t allow if some relation exists Nullify:

    Set relationship to nil Cascade: Delete related objects No Action: Do nothing, I want to cleanup Jedi Level
  12. Versioning and Migration MOM is your contract with Core Data

    If you change your model you must migrate the data to match the new data model Use a custom mapping model for changes Lightweight migration automatically makes minor changes (Snow Leopard/iOS 3.0+) Jedi Level
  13. Configurations A persistent store coordinator may only have one MOM

    One MOM may have many configurations A configuration is a subset of entities Typically used to store different entities in different stores using one store coordinator Jedi Level
  14. Resources Apple Documentation Core Data: Apple’s API for Persisting Data

    on Mac OS X by Marcus S. Zarra https://developer.apple.com/videos/wwdc/2010/