Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Advanced Core Data

Advanced Core Data

So you've been using Core Data in your apps and think it's great and simple and super powerful. But now you're starting to run into problems with your apps that you can't explain and possibly even performance issues. This session will cover some of the more advanced topics about Core Data including doing things in the background (concurrency), caching data, migrating schemas, and unit testing.

Aaron Douglas

August 25, 2014
Tweet

More Decks by Aaron Douglas

Other Decks in Programming

Transcript

  1. WHO I AM • Aaron Douglas • Milwaukee, WI USA

    • Mobile Maker for Automattic Inc. (WordPress.com) • Prior life Enterprise Java • @astralbodies
  2. ADVANCED CORE DATA TOPICS • Concurrency • Caching Data •

    Migrating Schemas • Performance • Unit Testing
  3. CORE DATA IN 30 SECONDS • Object Graph Store •

    Abstracts Persistence • Provides a lot for doing a little • Validation, faulting, paging, querying, versioning
  4. MAIN THREAD • Easy • Pretty fast • Xcode wizard

    template • Good enough for most apps & prototypes
  5. WHEN DO I WORRY? • Stuttering / Instruments Core Data

    • Asynchronous operations • Batch processing • Future-proofing app architecture
  6. THREADING CAVEATS • NSManagedObjects belong to a single context •

    Do not share between threads/contexts • Pass by NSManagedObjectID • managedObject.objectID.isTemporaryID
  7. IN THE BACKGROUND • Thread containment • Queues • Multiple

    contexts • Single persistent store coordinator
  8. THREAD CONTAINMENT • Each thread gets its own context •

    Manually manage contexts • Merge in changes from NSManagedObjectContext DidSaveNotification
  9. 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
  10. BACKGROUND FETCHING • NSPersistentStoreCoordinator • Background fetch to warm up

    the cache
 request.resultType = NSManagedObjectIDResultType • Full fetch on background thread - NSPersistentStoreCoordinator caching
  11. ENTITY HASH • Name, Inheritance, Persistent properties • Class name,

    transient properties, user info, validation predicates, default values • Hash modifier
  12. LIGHTWEIGHT MIGRATION • SQLite - all internal to db &

    no objects loaded into memory • Speedy • Light on memory
  13. INFER MAPPING MODEL • Not a silver bullet • Model

    upgrades can skip versions • Does not merge multiple versions • Business logic between upgrades is lost
  14. INFERRED LIMITATIONS • Add & Remove Attributes • Non-optional becomes

    optional • Optional becomes non-optional with default • Renaming entity or property
  15. 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
  16. TESTING • Test migrations LIKE CRAZY • Unit tests can

    help here! • Don’t assume current version only
  17. LOGGING • -com.apple.CoreData.SQLDebug 1 • Higher the number = more

    info • Loses usefulness pretty quick • Open SQLite file directly - Base.app
  18. XCTEST & CORE DATA • Unit testing code with Core

    Data is a pain • Managed Objects proliferate code • Test business logic, not necessarily Core Data
  19. WHAT DO I TEST? • Business logic • Integration with

    Core Data • Model Migrations • Concurrency • Timing / Performance
  20. 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