ADVANCED CORE DATA
AGENDA
Concurrency
Child & Parent Contexts
Migrations
CORE DATA &
CONCURRENCY
CORE DATA AND CONCURRENCY
Managed Object Contexts cannot be used from multiple
threads
Managed Objects cannot be passed between threads - you
need to refetch them on each thread using the
ManagedObjectID of the object you’re trying to retrieve
CORE DATA AND CONCURRENCY
Two different concurrency types for Managed Object Contexts:
NSMainQueueConcurrencyType contexts with this
concurrency type can only be used from the main thread
NSPrivateQueueConcurrencyType contexts with this
concurrency type can only be used from the private queue
they own; need to use performBlock to dispatch to private
queue
CORE DATA AND CONCURRENCY
let jsonArray = … //JSON data to be imported into Core Data
let moc = … //Our primary context on the main queue
let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType)
privateMOC.parentContext = moc
privateMOC.performBlock {
for jsonObject in jsonArray {
let mo = … //Managed object that matches the incoming JSON structure
//update MO with data from the dictionary
}
do {
try privateMOC.save()
} catch {
fatalError("Failure to save context: (error)")
}
}
Source: Example from Apple Documentation
CHILD & PARENT CONTEXTS
CHILD & PARENT CONTEXTS
Child Context
Main Context
Unsaved
Changes
Child Context
Main Context
Child Context
Main Context
Unsaved
Changes
New Unsaved Changes Child Context Saved Changes persisted
(1) (2) (3)
CHILD & PARENT CONTEXTS
Two main use cases for child contexts:
Threading: work with Core Data from a background thread
by using a Private Queue child context
Temporary changes: Allows users to discard changes without
saving them by keeping modifications in a child context
MIGRATIONS
MIGRATIONS
The data model of your application will change over time
You need to be able to load data from an older version of the
data model and migrate them to the new one
For small changes (adding properties, renaming properties,
etc.) Core Data can perform lightweight migrations out of the
box
Some changes require custom migrations
SUMMARY
SUMMARY
Core Data’s Managed Object Context and Managed Object classes
are not thread safe
Core Data can be used from background thread by using a separate
context with a private queue
A context can have a parent context - this is useful for threading and
change management
Core Data supports migrations, in many cases lightweight migrations
will work out of the box
ADDITIONAL RESOURCES
ADDITIONAL RESOURCES
Core Data Programming Guide: Concurrency
Core Data Model Versioning and Data Migration

Advanced Core Data

  • 2.
  • 3.
  • 4.
  • 5.
    CORE DATA ANDCONCURRENCY Managed Object Contexts cannot be used from multiple threads Managed Objects cannot be passed between threads - you need to refetch them on each thread using the ManagedObjectID of the object you’re trying to retrieve
  • 6.
    CORE DATA ANDCONCURRENCY Two different concurrency types for Managed Object Contexts: NSMainQueueConcurrencyType contexts with this concurrency type can only be used from the main thread NSPrivateQueueConcurrencyType contexts with this concurrency type can only be used from the private queue they own; need to use performBlock to dispatch to private queue
  • 7.
    CORE DATA ANDCONCURRENCY let jsonArray = … //JSON data to be imported into Core Data let moc = … //Our primary context on the main queue let privateMOC = NSManagedObjectContext(concurrencyType: .PrivateQueueConcurrencyType) privateMOC.parentContext = moc privateMOC.performBlock { for jsonObject in jsonArray { let mo = … //Managed object that matches the incoming JSON structure //update MO with data from the dictionary } do { try privateMOC.save() } catch { fatalError("Failure to save context: (error)") } } Source: Example from Apple Documentation
  • 8.
  • 9.
    CHILD & PARENTCONTEXTS Child Context Main Context Unsaved Changes Child Context Main Context Child Context Main Context Unsaved Changes New Unsaved Changes Child Context Saved Changes persisted (1) (2) (3)
  • 10.
    CHILD & PARENTCONTEXTS Two main use cases for child contexts: Threading: work with Core Data from a background thread by using a Private Queue child context Temporary changes: Allows users to discard changes without saving them by keeping modifications in a child context
  • 11.
  • 12.
    MIGRATIONS The data modelof your application will change over time You need to be able to load data from an older version of the data model and migrate them to the new one For small changes (adding properties, renaming properties, etc.) Core Data can perform lightweight migrations out of the box Some changes require custom migrations
  • 13.
  • 14.
    SUMMARY Core Data’s ManagedObject Context and Managed Object classes are not thread safe Core Data can be used from background thread by using a separate context with a private queue A context can have a parent context - this is useful for threading and change management Core Data supports migrations, in many cases lightweight migrations will work out of the box
  • 15.
  • 16.
    ADDITIONAL RESOURCES Core DataProgramming Guide: Concurrency Core Data Model Versioning and Data Migration