Core Data Neo
Outline Core Data Introduction Core Data Stack Core Data Operation Core Data Concurrency Experience Reference:
 https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/ CoreData/index.html
Core Data Introduction What is the Core Data Core Data is not RDBS, it’s ORM It’s a framework to manage the model layer object, not a DBMS Intelligent and efficiency data management tools Advantage in Core Data Efficiency data r/w, ex:lazy loading, faulting pre-fetching, etc… Intelligent data schema upgrade & migration Schema version control Data editing in memory, save changes back to store
Simple to know stack vs. RMDB Core Data Stack tables table configuration file.sql files
Core Data Stack
Establish a Core Data stack Core Data Stack
Core Data Stack
Core Data Operation
Core Data Operation Request data from fetchResultsController Request data from executeFetchRequest
Core Data Operation Insert new data to MOC Delete data from MOC Modify data in MOC Save change from MOC to PSC
Core Data Operation If you set delegate to fetchResultsController, then you can monitor what change in the MOC
Core Data Concurrency Core data supports concurrency operation and easy to implement, there are two ways Child Managed Object Context Another Managed Object Context but same Persistent Store Coordinator (Not recommend anymore) What time does we need it a lot of executions in core data, ex:insert, delete, or update A complex query which need a long time App is almost waiting for core data access The advantage is performance improvement when App has data access performance issue.
Core Data Concurrency Shouldn’t easy to try it if no performance issue Program would be more complex, and access data operation too. Managed Object Context is not thread-safe Data is needed to care about conflict and merge. Data is may not synced when miss some operations
Core Data Concurrency Select one concurrencyType when create MOC NSConfinementConcurrencyType Default type for backward, it’s for keeping MOC is accessed in the thread which it is created. (Deprecated in iOS 9.0) NSPrivateQueueConcurrencyType (child MOC) Create a private queue by GCD, it won’t block mainThread. NSMainQueueConcurrencyType (main MOC) MOC is accessed in mainThread, it would block UI.
Core Data Concurrency Child Concurrency architecture
Core Data Concurrency Asynchronous Saving When you have heavy I/O in core data
Core Data Concurrency Operation of child MOC is the same as parent MOC, but there are something important Pass the MO to different MOC that is forbidden Child MOC save just update data of parent MOC, it won’t save data to disk. Before child MOC save, data of parent MOC won’t have any change, also parent MOC can’t find data which belong to child MOC. Is only the architecture? NO, but I don’t think that need more concurrency architectures in the most normal cases.
Experience We got the Core Data fundamental now, but something which need to be known in my experience
Experience Pass MO between different contexts We can’t pass MO directly Use NSManagedObjectID to access MO from persistentStore or memory. MO status maybe is not consistent
 ex: MO is changed in child MOC without save, and the change won’t affect the MO in parent MOC. NSManagedObjectID has two status PersistentID
 The MO is saved back to persistent store, and can be accessed anywhere by objectID TemporaryID
 The MO is no saved before, and it can’t be accessed from context.
 After saved MO, you can’t find object by the ID!!
 BTW, you can check the ID status by isTemporaryID
Experience There are three methods access MO by objectID objectWithID
 Always return a MO, whatever it has data or not. 
 It can accessed parent’s temp MO in child, but that’s a new one.
 Don’t use return value as Bool, it always get a empty data MO!! objectRegisteredForID
 Return MO if existed in MOC, otherwise return nil. 
 Ex: a MO saved in parent MOC, the method can’t access it in child MOC. existingObjectWithID
 Return MO if it has been saved before, otherwise return nil.
Experience MO has some status properties isFault, isDeleted, faultingState, isInserted, isUpdated, isTemporaryID, hasChanges These status can be checked what they do in context These status would be reset after saved isDeleted If delete MO and it’s no saved before, then the MO would be nil directly If delete MO and it’s saved before, then only isDelete property would be changed
 After deleted MO, it always existed if it was saved before!! Check MO is deleted Sometimes isDeleted can’t really know MO is deleted or not Using existingObjectWithID and managedObjectContext
 Check condition should be: (MO.isDelete || ![MOC exist..ID:MOID] || ! MO.managedObjectContext)
Experience MO Update Without save, change MO in the child MOC won’t affect attribute/status MO in the parent MOC, otherwise, too. Without save, insert MO in the child MOC won’t affect MO in the parent MOC, otherwise, child MOC would be affected. You can try core data operation in my testbed https://github.com/flamelad/CoreDataTestbed
Q & A

Core Data Introduction

  • 1.
  • 2.
    Outline Core Data Introduction CoreData Stack Core Data Operation Core Data Concurrency Experience Reference:
 https://developer.apple.com/library/watchos/documentation/Cocoa/Conceptual/ CoreData/index.html
  • 3.
    Core Data Introduction Whatis the Core Data Core Data is not RDBS, it’s ORM It’s a framework to manage the model layer object, not a DBMS Intelligent and efficiency data management tools Advantage in Core Data Efficiency data r/w, ex:lazy loading, faulting pre-fetching, etc… Intelligent data schema upgrade & migration Schema version control Data editing in memory, save changes back to store
  • 4.
    Simple to knowstack vs. RMDB Core Data Stack tables table configuration file.sql files
  • 5.
  • 6.
    Establish a CoreData stack Core Data Stack
  • 7.
  • 8.
  • 9.
    Core Data Operation Requestdata from fetchResultsController Request data from executeFetchRequest
  • 10.
    Core Data Operation Insertnew data to MOC Delete data from MOC Modify data in MOC Save change from MOC to PSC
  • 11.
    Core Data Operation Ifyou set delegate to fetchResultsController, then you can monitor what change in the MOC
  • 12.
    Core Data Concurrency Coredata supports concurrency operation and easy to implement, there are two ways Child Managed Object Context Another Managed Object Context but same Persistent Store Coordinator (Not recommend anymore) What time does we need it a lot of executions in core data, ex:insert, delete, or update A complex query which need a long time App is almost waiting for core data access The advantage is performance improvement when App has data access performance issue.
  • 13.
    Core Data Concurrency Shouldn’teasy to try it if no performance issue Program would be more complex, and access data operation too. Managed Object Context is not thread-safe Data is needed to care about conflict and merge. Data is may not synced when miss some operations
  • 14.
    Core Data Concurrency Selectone concurrencyType when create MOC NSConfinementConcurrencyType Default type for backward, it’s for keeping MOC is accessed in the thread which it is created. (Deprecated in iOS 9.0) NSPrivateQueueConcurrencyType (child MOC) Create a private queue by GCD, it won’t block mainThread. NSMainQueueConcurrencyType (main MOC) MOC is accessed in mainThread, it would block UI.
  • 15.
    Core Data Concurrency ChildConcurrency architecture
  • 16.
    Core Data Concurrency AsynchronousSaving When you have heavy I/O in core data
  • 17.
    Core Data Concurrency Operationof child MOC is the same as parent MOC, but there are something important Pass the MO to different MOC that is forbidden Child MOC save just update data of parent MOC, it won’t save data to disk. Before child MOC save, data of parent MOC won’t have any change, also parent MOC can’t find data which belong to child MOC. Is only the architecture? NO, but I don’t think that need more concurrency architectures in the most normal cases.
  • 18.
    Experience We got theCore Data fundamental now, but something which need to be known in my experience
  • 19.
    Experience Pass MO betweendifferent contexts We can’t pass MO directly Use NSManagedObjectID to access MO from persistentStore or memory. MO status maybe is not consistent
 ex: MO is changed in child MOC without save, and the change won’t affect the MO in parent MOC. NSManagedObjectID has two status PersistentID
 The MO is saved back to persistent store, and can be accessed anywhere by objectID TemporaryID
 The MO is no saved before, and it can’t be accessed from context.
 After saved MO, you can’t find object by the ID!!
 BTW, you can check the ID status by isTemporaryID
  • 20.
    Experience There are threemethods access MO by objectID objectWithID
 Always return a MO, whatever it has data or not. 
 It can accessed parent’s temp MO in child, but that’s a new one.
 Don’t use return value as Bool, it always get a empty data MO!! objectRegisteredForID
 Return MO if existed in MOC, otherwise return nil. 
 Ex: a MO saved in parent MOC, the method can’t access it in child MOC. existingObjectWithID
 Return MO if it has been saved before, otherwise return nil.
  • 21.
    Experience MO has somestatus properties isFault, isDeleted, faultingState, isInserted, isUpdated, isTemporaryID, hasChanges These status can be checked what they do in context These status would be reset after saved isDeleted If delete MO and it’s no saved before, then the MO would be nil directly If delete MO and it’s saved before, then only isDelete property would be changed
 After deleted MO, it always existed if it was saved before!! Check MO is deleted Sometimes isDeleted can’t really know MO is deleted or not Using existingObjectWithID and managedObjectContext
 Check condition should be: (MO.isDelete || ![MOC exist..ID:MOID] || ! MO.managedObjectContext)
  • 22.
    Experience MO Update Without save,change MO in the child MOC won’t affect attribute/status MO in the parent MOC, otherwise, too. Without save, insert MO in the child MOC won’t affect MO in the parent MOC, otherwise, child MOC would be affected. You can try core data operation in my testbed https://github.com/flamelad/CoreDataTestbed
  • 23.