Slide 1

Slide 1 text

ADVANCED CORE DATA The Things You Thought You Could Ignore Much Wow.

Slide 2

Slide 2 text

WHO I AM • Aaron Douglas • Milwaukee, WI USA • Mobile Maker for Automattic Inc. (WordPress.com) • Prior life Enterprise Java • @astralbodies

Slide 3

Slide 3 text

ADVANCED CORE DATA TOPICS • Concurrency • Caching Data • Migrating Schemas • Performance • Unit Testing

Slide 4

Slide 4 text

ASK QUESTIONS This talk is all about you!

Slide 5

Slide 5 text

CORE DATA IN 30 SECONDS • Object Graph Store • Abstracts Persistence • Provides a lot for doing a little • Validation, faulting, paging, querying, versioning

Slide 6

Slide 6 text

CONCURRENCY “It’s so simple!”

Slide 7

Slide 7 text

MAIN THREAD • Easy • Pretty fast • Xcode wizard template • Good enough for most apps & prototypes

Slide 8

Slide 8 text

WHEN DO I WORRY? • Stuttering / Instruments Core Data • Asynchronous operations • Batch processing • Future-proofing app architecture

Slide 9

Slide 9 text

THREADING CAVEATS • NSManagedObjects belong to a single context • Do not share between threads/contexts • Pass by NSManagedObjectID • managedObject.objectID.isTemporaryID

Slide 10

Slide 10 text

IN THE BACKGROUND • Thread containment • Queues • Multiple contexts • Single persistent store coordinator

Slide 11

Slide 11 text

THREAD CONTAINMENT • Each thread gets its own context • Manually manage contexts • Merge in changes from NSManagedObjectContext DidSaveNotification

Slide 12

Slide 12 text

QUEUES • NSManagedObjectContext initWithConcurrencyType: • NSMainQueueConcurrencyType • NSPrivateQueueConcurrencyType • NSConfinementConcurrencyType • Parent context • NSManagedObjectContextDidSaveNotification

Slide 13

Slide 13 text

QUEUES • performBlock - immediately returns • performWithBlockAndWait • Main thread can still execute directly

Slide 14

Slide 14 text

PREFERRED SETUP • Root/Master: Private Queue • UI: Main Queue as child of Master Context • Worker: Private Queue as child of Main Context • Allows for asynchronous saves http://floriankugler.com/blog/2013/4/2/the-concurrent-core-data-stack

Slide 15

Slide 15 text

CHALLENGES • Saving • Merging • Conflicts

Slide 16

Slide 16 text

SAVING • NSManagedObjectContextDidSaveNotification • Save in one spot • Handling problems

Slide 17

Slide 17 text

MERGING • NSMergePolicy • NSErrorMergePolicy - default • NSMergeByPropertyStoreTrumpMergePolicy • NSMergeByPropertyObjectTrumpMergePolicy • NSOverwriteMergePolicy

Slide 18

Slide 18 text

CONFLICTS • NSErrorMergePolicy • NSError userInfo[@“conflictList”] • User probably needs to decide • UX is key!

Slide 19

Slide 19 text

DEMO Concurrency In Action

Slide 20

Slide 20 text

CACHING DATA

Slide 21

Slide 21 text

FETCHED RESULTS CONTROLLER • NSFetchedResultsController • Listens for context changes • Cache name & deleteCacheWithName:

Slide 22

Slide 22 text

BACKGROUND FETCHING • NSPersistentStoreCoordinator • Background fetch to warm up the cache
 request.resultType = NSManagedObjectIDResultType • Full fetch on background thread - NSPersistentStoreCoordinator caching

Slide 23

Slide 23 text

MIGRATING SCHEMAS

Slide 24

Slide 24 text

VERSIONING • Why use it? • Version number - hint • Hashes

Slide 25

Slide 25 text

ENTITY HASH • Name, Inheritance, Persistent properties • Class name, transient properties, user info, validation predicates, default values • Hash modifier

Slide 26

Slide 26 text

AUTOMATIC MIGRATION • Infer Mapping Model • Migrate Store Automatically

Slide 27

Slide 27 text

LIGHTWEIGHT MIGRATION • SQLite - all internal to db & no objects loaded into memory • Speedy • Light on memory

Slide 28

Slide 28 text

HEAVYWEIGHT MIGRATION • Every object loaded into memory • Manually map and manipulate data

Slide 29

Slide 29 text

INFER MAPPING MODEL • Not a silver bullet • Model upgrades can skip versions • Does not merge multiple versions • Business logic between upgrades is lost

Slide 30

Slide 30 text

App Version Model Version 1.0 1 1.1 1 2.0 2 3.0 3

Slide 31

Slide 31 text

App Version Model Version 1.0 1 3.0 3 NSInferMappingModelAutomaticallyOption

Slide 32

Slide 32 text

INFERRED LIMITATIONS • Add & Remove Attributes • Non-optional becomes optional • Optional becomes non-optional with default • Renaming entity or property

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

TESTING • Test migrations LIKE CRAZY • Unit tests can help here! • Don’t assume current version only

Slide 35

Slide 35 text

PERFORMANCE

Slide 36

Slide 36 text

INSTRUMENTS • Run on the device • Fetches • Saves • Faults • Cache Misses

Slide 37

Slide 37 text

LOGGING • -com.apple.CoreData.SQLDebug 1 • Higher the number = more info • Loses usefulness pretty quick • Open SQLite file directly - Base.app

Slide 38

Slide 38 text

PREDICATES • contains • endsWith • like • matches • non-text first

Slide 39

Slide 39 text

MEMORY • Autorelease pools • NSManagedObjectContext reset • NSManagedObjectContext refreshObject:mergeChanges:

Slide 40

Slide 40 text

TESTING

Slide 41

Slide 41 text

XCTEST & CORE DATA • Unit testing code with Core Data is a pain • Managed Objects proliferate code • Test business logic, not necessarily Core Data

Slide 42

Slide 42 text

WHAT DO I TEST? • Business logic • Integration with Core Data • Model Migrations • Concurrency • Timing / Performance

Slide 43

Slide 43 text

DEMO Unit Testing Core Data

Slide 44

Slide 44 text

IOS 8 CHANGES • NSBatchUpdateRequest • Cloud Kit • Under the hood

Slide 45

Slide 45 text

QUESTIONS?

Slide 46

Slide 46 text

REFERENCES • Core Data Programming Guide - Apple
 This documentation is seriously out of date.
 No really.
 It’s bad. (better with iOS 8) • Core Data 2nd Ed - Marcus Zarra • RayWenderlich.com tutorials

Slide 47

Slide 47 text

THANKS! Contact Information Aaron Douglas @astralbodies http://astralbodi.es