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

Daniel Eggert's Slides at NSSpain 2013

Luis Ascorbe
September 18, 2013
200

Daniel Eggert's Slides at NSSpain 2013

NSSpain is an iOS & OSX developers conference in Spain.
More info: http://nsspain.com

Luis Ascorbe

September 18, 2013
Tweet

Transcript

  1. Agenda • What is Core Data? • Core Data Stack

    & Data Model • Relationships • Getting Data from Core Data • Performance • Advanced Core Data
  2. What is Core Data? • Objective-C • Mature Code Base

    — 8 years in the wild • OS X since 10.4 • iOS since iOS 3 • No UI
  3. What is Core Data? • (Very powerful) object graph management

    • Persistency technology • Not an SQL wrapper • Not a Object-relational mapping (O/RM)
  4. NSPersistent StoreCoordinator SQLite File System NSManagedObjectContext NSManaged Object NSManaged Object

    NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store NSPersistent Store NSPersistent Store SQLite File System SQLite File System Core Data Stack
  5. Core Data Stack NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object

    NSManaged Object NSManaged Object NSPersistent Store SQLite File System
  6. Core Data Stack NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object

    NSManaged Object NSManaged Object NSPersistent Store SQLite File System { { Object Graph Management Persistence
  7. Core Data Stack NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object

    NSManaged Object NSManaged Object NSPersistent Store SQLite File System NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object
  8. entity |ˈɛntɪti| noun ( pl. entities ) a thing with

    distinct and independent existence: Church and empire were fused in a single entity. “Route” Entity “City” Entity
  9. Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain

    Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain Data
  10. Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain

    Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain
  11. Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain

    Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain Boring Part
  12. Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain

    Berlin Bilbao Logroño Paris San Francisco Copenhagen Denmark WWDC NSSpain
  13. Berlin Bilbao Logroño Denmark WWDC NSSpain Relationships cities = nsspain.cities;

    Berlin Bilbao Logroño NSSpain berlin = cities[0]; otherRoutes = berlin.routes;
  14. Relationships Route *nsspain = ... NSOrderedSet *cities = nsspain.cities; City

    *berlin = cities[0]; NSSet *otherRoutes = berlin.routes;
  15. Searching • NSFetchRequest allows you to set a predicate •

    NSPredicate specify what you want to fetch • Target your search • Less I/O → better performance • Only bring in those objects you want • Core Data doesn’t have to pull in data you don’t need
  16. All Objects • Showing a list of all objects to

    the user • UITableView • UICollectionView • You must not bring in all objects at once • You must not bring in each object on its own
  17. Pre-Fetching • Round tripping to the Database is Expensive •

    Fetch as much as possible in 1 go NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store SQLite File System
  18. Pre-Fetching • Execute an NSFetchRequest without using its result. Just

    to bring objects into the context • Only do this when you need to. • Don’t fetch objects if they’re already in the context (or the persistent store coordinator’s row cache) • Measure • Don’t prematurely optimize
  19. Performance Issues Top 3 • Loading too much data •

    Not loading enough data • Inefficient queries These 3 are a problem in any database application, not just Core Data, but people like to blame Core Data.
  20. Loading too much data • Loading everything is BAD •

    batching • aggregating • Sorting and filtering in memory is BAD • predicates • sort descriptors
  21. Not loading enough data • Round trips to the file

    system are SLOW • Relationship prefetching
  22. Inefficient Queries • Unicode text searching is expensive, but necessary

    • normalize text (uppercase, diacritics) • ‘like’ is expensive! • beginswith and contains are a lot faster than like
  23. Inefficient Queries • Order of predicates matters! • Cheap stuff

    at the beginning • Relationships at the end • Xcode Documentation: “Predicates” Guide
  24. Multi-Context Multi-Store Core Data support various, complex setups NSPersistent StoreCoordinator

    NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store SQLite File System
  25. Multi-Context NSPersistent StoreCoordinator SQLite File System NSManagedObjectContext NSManaged Object NSManaged

    Object NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store Contexts are distinct — distinct changes. NSManagedObject tied to their owning context.
  26. Multi-Store No relations across stores. NSPersistent StoreCoordinator SQLite File System

    NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store NSPersistent Store NSPersistent Store SQLite File System SQLite File System
  27. Multi-Stack NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object

    NSManaged Object NSPersistent Store SQLite File System NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store SQLite File System
  28. Mixing everything NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged

    Object NSManaged Object NSPersistent Store SQLite File System NSPersistent StoreCoordinator SQLite File System NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store NSPersistent Store NSPersistent Store SQLite File System SQLite File System
  29. Complex Stacks • Very, very flexible • Also adds a

    lot of complexity • Threading is always complex • You NEED a merging policy • You can almost always do without • Save yourself the trouble — go for simple
  30. Parent-Child Context • Don’t use it — it’s more complicated

    than it seems • Implemented to facilitate deferred saving (UIManagedDocument) NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store SQLite File System NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object
  31. Parent-Child Context • There’s a lot of locking that NEEDS

    to happen • NSManagedObjectID behaves differently NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object NSPersistent Store SQLite File System NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged Object
  32. NSPersistent StoreCoordinator NSManagedObjectContext NSManaged Object NSManaged Object NSManaged Object NSManaged

    Object Your own code here ? Incremental Store • Transactional ?!? • Threading / Async ?!?
  33. Why Core Data? • Relationships • Delete Propagation • Change

    Tracking and Undo • Scratchpad • Faulting • Uniquing
  34. Why Core Data? • Very mature code base — more

    than 8 years • Battle tested • Very performant if used right
  35. Covered Today • What is Core Data? • Core Data

    Stack & Data Model • Relationships • Getting Data from Core Data • Performance • Advanced Core Data
  36. More Information • Check the Xcode Documentation • “Core Data

    Starting Point” • “Core Data Programming Guide” • “Core Data Snippets” • http://www.objc.io/issue-4/