Transcript
Page 1: Advanced Core Data - The Things You Thought You Could Ignore

ADVANCED CORE DATAThe Things You Thought You Could Ignore

Much Wow.

Page 2: Advanced Core Data - The Things You Thought You Could Ignore

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

ADVANCED CORE DATA TOPICS

• Concurrency

• Caching Data

• Migrating Schemas

• Undo Management

• Performance

Page 4: Advanced Core Data - The Things You Thought You Could Ignore

ASK QUESTIONSThis talk is all about you!

Page 5: Advanced Core Data - The Things You Thought You Could Ignore

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

CONCURRENCY“It’s so simple!”

Page 7: Advanced Core Data - The Things You Thought You Could Ignore

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

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

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

IN THE BACKGROUND

• Thread containment

• Queues

• Multiple contexts

• Single persistent store coordinator

Page 11: Advanced Core Data - The Things You Thought You Could Ignore

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

QUEUES• NSManagedObjectContext initWithConcurrencyType:

• NSMainQueueConcurrencyType

• NSPrivateQueueConcurrencyType

• NSConfinementConcurrencyType

• Parent context

• NSManagedObjectContextDidSaveNotification

Page 13: Advanced Core Data - The Things You Thought You Could Ignore

QUEUES

• performBlock - immediately returns

• performWithBlockAndWait

• Main thread can still execute directly

Page 14: Advanced Core Data - The Things You Thought You Could Ignore

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

CHALLENGES

• Saving

• Merging

• Conflicts

Page 16: Advanced Core Data - The Things You Thought You Could Ignore

SAVING

• NSManagedObjectContextDidSaveNotification

• Save in one spot

• Handling problems

Page 17: Advanced Core Data - The Things You Thought You Could Ignore

MERGING• NSMergePolicy

• NSErrorMergePolicy - default

• NSMergeByPropertyStoreTrumpMergePolicy

• NSMergeByPropertyObjectTrumpMergePolicy

• NSOverwriteMergePolicy

Page 18: Advanced Core Data - The Things You Thought You Could Ignore

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

CACHING DATA

Page 20: Advanced Core Data - The Things You Thought You Could Ignore

FETCHED RESULTS CONTROLLER

• NSFetchedResultsController

• Listens for context changes

• Cache name & deleteCacheWithName:

Page 21: Advanced Core Data - The Things You Thought You Could Ignore

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

MIGRATING SCHEMAS

Page 23: Advanced Core Data - The Things You Thought You Could Ignore

VERSIONING

• Why use it?

• Version number - hint

• Hashes

Page 24: Advanced Core Data - The Things You Thought You Could Ignore

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

AUTOMATIC MIGRATION

• Infer Mapping Model

• Migrate Store Automatically

Page 26: Advanced Core Data - The Things You Thought You Could Ignore

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

HEAVYWEIGHT MIGRATION

• Every object loaded into memory

• Manually map and manipulate data

Page 28: Advanced Core Data - The Things You Thought You Could Ignore

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

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

App Version

Model Version

1.0

1

3.0

3NSInferMappingModelAutomaticallyOption

Page 31: Advanced Core Data - The Things You Thought You Could Ignore

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

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

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

UNDO MANAGEMENT

Page 35: Advanced Core Data - The Things You Thought You Could Ignore

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

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

PERFORMANCE

Page 38: Advanced Core Data - The Things You Thought You Could Ignore

INSTRUMENTS• Run on the device

• Fetches

• Saves

• Faults

• Cache Misses

Page 39: Advanced Core Data - The Things You Thought You Could Ignore

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

PREDICATES• contains

• endsWith

• like

• matches

• non-text first

Page 41: Advanced Core Data - The Things You Thought You Could Ignore

MEMORY

• Autorelease pools

• NSManagedObjectContext reset

• NSManagedObjectContext refreshObject:mergeChanges:

Page 42: Advanced Core Data - The Things You Thought You Could Ignore

QUESTIONS?

Page 43: Advanced Core Data - The Things You Thought You Could Ignore

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

THANKS!

Contact Information

Aaron Douglas

@astralbodies

http://github.com/astralbodies


Top Related