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

Everyday CoreData

Everyday CoreData

My CoreData talk at CodersOnly on March 16, 2016

Pat Zearfoss

March 16, 2016
Tweet

More Decks by Pat Zearfoss

Other Decks in Technology

Transcript

  1. About Me • Pat Zearfoss • CircleBack on the app

    store • Running on a CoreData stack for better than a year. • @patzearfoss on Twitter.
  2. End User License Agreement • My suggestions may not be

    100% right for all situations • CoreData elicits a lot of opinions in the community. • Your app may not fit the same patterns and models
  3. Things I’ll talk about • Frameworks and tools • Model

    design • App architecture • Testing • Debugging tools
  4. CoreData Review • A way of persisting data in Cocoa

    applications • Not a database • Well, it kind of is, but not necessarily • On label usage is “object graph persistence”
  5. Why • It’ll help you simplify all the stuff you’ll

    do anyway. • CoreData can be . . . wordy.
  6. Creating a new instance if let eDesc = NSEntityDescription.entityForName(“Contact”, inManagedObjectContext:

    context) { let contact1 = Contact(entity: contactDescription, insertIntoManagedObjectContext:context) }
  7. A basic fetch let fetchRequest = NSFetchRequest(entityName: “Contact”) fetchRequest.sortDescriptors =

    [NSSortDescriptor(key: “lastName”, ascending: true)] fetchRequest.predicate = NSPredicate(format: “isFavorite = YES”) if let context = context { do { try contacts = context. executeFetchRequest(fetchRequest) as! [Contact] } catch { print (“couldn’t load contacts”) } }
  8. Creating an instance - MR if let contact1 = Contact.MR_createEntityInContext(context)

    Fetch if let fetchedContacts = Contact.MR_findAllSortedBy(“lastName”, ascending: true, inContext: context) as? [Contact]
  9. Modeling • Your managed model is a database, but it

    also isn’t. You have to make it work right for you.
  10. Tip #3 Use NSManagedObjectContexts liberally • Creating a new context

    to fetch objects, passing the ids to a main context for use on the UI. • Doing expensive operations in background queues • Creating a throw-away editing context.
  11. NSFetchedResultsController • Keeping a UI updated with changes from the

    internet. • Use fetched results controllers to manage all communication with a web backend.
  12. Testing • You don’t really care about persistence • You

    just want to test this object, controller, view model, etc.
  13. Tip #5 Use an in-memory store for testing • Sets

    up the same scheme you have in memory. • Quick and easy to set up an tear down between tests
  14. Useful environment variables • com.apple.CoreData.SQLDebug [1,2,3] • com.apple.CoreData.SyntaxColoredLogging 1 •

    com.apple.CoreData.ConcurrencyDebug 1 • (not ThreadDebug) • com.apple.CoreData.MigrationDebug
  15. Tip #8 Other people have really good ideas • Saul

    Mora - original author of MagicalRecord • Marcus Zarra - wrote many a book on CoreData • objc.io on CoreData • WWDC Material (obvi)
  16. Recap • Find a good framework to help you. •

    Design a model that fits your app’s needs • Make use of NSManagedObjectContext • Use NSFetchedResultsControllers
  17. Recap • Use in memory stores for testing • CoreData

    runtime arguments are your friend • Make use of Instruments • Read good material