Advanced Core Data

16

Transcript of Advanced Core Data

Page 1: Advanced Core Data
Page 2: Advanced Core Data

ADVANCED CORE DATA

Page 3: Advanced Core Data

AGENDA

Concurrency

Child & Parent Contexts

Migrations

Page 4: Advanced Core Data

CORE DATA & CONCURRENCY

Page 5: Advanced Core Data

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

Page 6: Advanced Core Data

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

Page 7: Advanced Core Data

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

Page 8: Advanced Core Data

CHILD & PARENT CONTEXTS

Page 9: Advanced Core Data

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)

Page 10: Advanced Core Data

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

Page 11: Advanced Core Data

MIGRATIONS

Page 12: Advanced Core Data

MIGRATIONSThe 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

Page 13: Advanced Core Data

SUMMARY

Page 14: Advanced Core Data

SUMMARYCore 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

Page 15: Advanced Core Data

ADDITIONAL RESOURCES