![Page 1: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/1.jpg)
ADVANCED CORE DATAThe Things You Thought You Could Ignore
Much Wow.
![Page 2: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/2.jpg)
WHO I AM
• Aaron Douglas
• Milwaukee, WI USA
• Mobile Maker for Automattic Inc. (WordPress.com)
• Prior life Enterprise Java
• @astralbodies
![Page 3: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/3.jpg)
ADVANCED CORE DATA TOPICS
• Concurrency
• Caching Data
• Migrating Schemas
• Undo Management
• Performance
![Page 4: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/4.jpg)
ASK QUESTIONSThis talk is all about you!
![Page 5: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/5.jpg)
CORE DATA IN 30 SECONDS
• Object Graph Store
• Abstracts Persistence
• Provides a lot for doing a little
• Validation, faulting, paging, querying, versioning
![Page 6: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/6.jpg)
CONCURRENCY“It’s so simple!”
![Page 7: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/7.jpg)
MAIN THREAD
• Easy
• Pretty fast
• Xcode wizard template
• Good enough for most apps & prototypes
![Page 8: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/8.jpg)
WHEN DO I WORRY?
• Stuttering / Instruments 👉 Core Data 👎
• Asynchronous operations
• Batch processing
• Future-proofing app architecture
![Page 9: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/9.jpg)
THREADING CAVEATS
• NSManagedObjects belong to a single context
• Do not share between threads/contexts
• Pass by NSManagedObjectID
• [[managedObject objectID] isTemporaryID]
![Page 10: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/10.jpg)
IN THE BACKGROUND
• Thread containment
• Queues
• Multiple contexts
• Single persistent store coordinator
![Page 11: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/11.jpg)
THREAD CONTAINMENT
• Each thread gets its own context
• Manually manage contexts
• Merge in changes from NSManagedObjectContextDidSaveNotification
![Page 12: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/12.jpg)
QUEUES• NSManagedObjectContext initWithConcurrencyType:
• NSMainQueueConcurrencyType
• NSPrivateQueueConcurrencyType
• NSConfinementConcurrencyType
• Parent context
• NSManagedObjectContextDidSaveNotification
![Page 13: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/13.jpg)
QUEUES
• performBlock - immediately returns
• performWithBlockAndWait
• Main thread can still execute directly
![Page 14: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/14.jpg)
PREFERRED SETUP
• Primary: Private Queue
• UI: Main Queue as child of Primary
• Background: Private Queue as child of Primary
• Allows for asynchronous saves
http://floriankugler.com/blog/2013/4/2/the-concurrent-core-data-stack
![Page 15: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/15.jpg)
CHALLENGES
• Saving
• Merging
• Conflicts
![Page 16: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/16.jpg)
SAVING
• NSManagedObjectContextDidSaveNotification
• Save in one spot
• Handling problems
![Page 17: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/17.jpg)
MERGING• NSMergePolicy
• NSErrorMergePolicy - default
• NSMergeByPropertyStoreTrumpMergePolicy
• NSMergeByPropertyObjectTrumpMergePolicy
• NSOverwriteMergePolicy
![Page 18: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/18.jpg)
CONFLICTS
• NSErrorMergePolicy
• NSError userInfo[@“conflictList”]
• User probably needs to decide
• UX is key!
![Page 19: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/19.jpg)
CACHING DATA
![Page 20: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/20.jpg)
FETCHED RESULTS CONTROLLER
• NSFetchedResultsController
• Listens for context changes
• Cache name & deleteCacheWithName:
![Page 21: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/21.jpg)
BACKGROUND FETCHING
• NSPersistentStoreCoordinator
• Background fetch to warm up the cacherequest.resultType = NSManagedObjectIDResultType
• Full fetch on background thread - NSPersistentStoreCoordinator caching
![Page 22: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/22.jpg)
MIGRATING SCHEMAS
![Page 23: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/23.jpg)
VERSIONING
• Why use it?
• Version number - hint
• Hashes
![Page 24: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/24.jpg)
ENTITY HASH
• Name, Inheritance, Persistent properties
• Class name, transient properties, user info, validation predicates, default values
• Hash modifier
![Page 25: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/25.jpg)
AUTOMATIC MIGRATION
• Infer Mapping Model
• Migrate Store Automatically
![Page 26: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/26.jpg)
LIGHTWEIGHT MIGRATION
• SQLite - all internal to db & no objects loaded into memory
• Speedy
• Light on memory
![Page 27: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/27.jpg)
HEAVYWEIGHT MIGRATION
• Every object loaded into memory
• Manually map and manipulate data
![Page 28: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/28.jpg)
INFER MAPPING MODEL
• Not a silver bullet
• Model upgrades can skip versions
• Does not merge multiple versions
• Business logic between upgrades is lost
![Page 29: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/29.jpg)
App Version
Model Version
1.0
1
1.1
1
2.0
2
3.0
3
![Page 30: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/30.jpg)
App Version
Model Version
1.0
1
3.0
3NSInferMappingModelAutomaticallyOption
![Page 31: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/31.jpg)
INFERRED LIMITATIONS
• Add & Remove Attributes
• Non-optional becomes optional
• Optional becomes non-optional with default
• Renaming entity or property
![Page 32: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/32.jpg)
MANUAL MAPPING
• More complex scenarios
• Mapping model is for specific version to version
• Multiple version change not support unless sequential migrations used
• Code is needed for sequential migrations
![Page 33: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/33.jpg)
TESTING
• Test migrations LIKE CRAZY
• Unit tests can help here!
• Don’t assume current version only
![Page 34: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/34.jpg)
UNDO MANAGEMENT
![Page 35: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/35.jpg)
UNDO MANAGEMENT
• NSUndoManager
• Built-in support in NSManagedObjectContext
• Undo manager is nil in iOS
• Simple to use, easy to mess up
![Page 36: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/36.jpg)
Usage
NSUndoManager *undoManager = [[NSUndoManager alloc] init]; !undoManager.levelsOfUndo = 10; !context.undoManager = undoManager; !... ![context.undoManager undo];
![Page 37: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/37.jpg)
PERFORMANCE
![Page 38: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/38.jpg)
INSTRUMENTS• Run on the device
• Fetches
• Saves
• Faults
• Cache Misses
![Page 39: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/39.jpg)
LOGGING
• -com.apple.CoreData.SQLDebug 1
• Higher the number = more info
• Loses usefulness pretty quick
• Open SQLite file directly - Base.app
![Page 40: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/40.jpg)
PREDICATES• contains
• endsWith
• like
• matches
• non-text first
![Page 41: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/41.jpg)
MEMORY
• Autorelease pools
• NSManagedObjectContext reset
• NSManagedObjectContext refreshObject:mergeChanges:
![Page 42: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/42.jpg)
QUESTIONS?
![Page 43: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/43.jpg)
REFERENCES
• Core Data Programming Guide - AppleThis documentation is seriously out of date.No really.It’s bad.
• Core Data 2nd Ed - Marcus Zarra
![Page 44: Advanced Core Data - The Things You Thought You Could Ignore](https://reader034.vdocuments.net/reader034/viewer/2022052505/55519866b4c905013a8b4a22/html5/thumbnails/44.jpg)
THANKS!
Contact Information
Aaron Douglas
@astralbodies
http://github.com/astralbodies