"CoreData tips &tricks" by Anton Fedorchenko

"CoreData tips &tricks" by Anton Fedorchenko

Since it's early release in iOS 3.2 CoreData has become one of the most challenging frameworks for iOS developers. Building the correct data model and it's efficient maintenance mechanisms are still key task to build powerful and performant app. CoreData is also on of the most criticized frameworks, especially after releasing competitors like Realm / Couchbase. Few tips & tricks to give your CoreData more power to compete will be revealed in this talk.
This talk was made for CocoaHeads Kyiv #10 which took place 10 Dec 2016.

Db84cf61fdada06b63f43f310b68b462?s=128

CocoaHeads Ukraine

January 03, 2017
Tweet

Transcript

  1. Core Data Tips & Tricks Anton Fedorchenko Senior iOS Engineer

    / TL, Lohika
  2. Agenda • Core Data Introduction • What’s New in Core

    Data • Stack Setup • Demo • Debugging & Testing • Migration • Tips
  3. Introduction • What’s Core Data

  4. Introduction • What’s Core Data • Key features

  5. Introduction • What’s Core Data • Key features • Faulting

    Deserts Appetizers Guacamole Ceviche Menu
  6. Introduction • What’s Core Data • Key features • Faulting

    Deserts Appetizers Guacamole Ceviche Menu
  7. Introduction • What’s Core Data • Key features • Faulting

    Deserts Appetizers Guacamole Ceviche Menu
  8. Introduction • What’s Core Data • Key features • Faulting

    Deserts Appetizers Ceviche Menu
  9. Introduction • What’s Core Data • Key features • Faulting

    Deserts Appetizers Menu
  10. Introduction • What’s Core Data • Key features • Faulting

    • Attributes validation
  11. Introduction • What’s Core Data • Key features • Faulting

    • Attributes validation • Schema migration
  12. Introduction • What’s Core Data • Key features • Faulting

    • Attributes validation • Schema migration • Interface synchronization Model
  13. Introduction • What’s Core Data • Key features • Faulting

    • Attributes validation • Schema migration • Interface synchronization • Sophisticated queries
  14. Introduction • What’s Core Data • Key features • Why

    Core Data???
  15. What’s New

  16. What’s New • NSPersistentContainer

  17. What’s New • NSPersistentContainer • Concurrency changes

  18. What’s New

  19. What’s New • NSPersistentContainer • Concurrency changes • Query generations

    for transient versioning
  20. What’s New • NSPersistentContainer • Concurrency changes • Query generations

    for transient versioning • Xcode automatic subclass generation
  21. What’s New • NSPersistentContainer • Concurrency changes • Query generations

    for transient versioning • Xcode automatic subclass generation • Finally added missed methods on NSManagedObject
  22. What’s New • NSPersistentContainer • Concurrency changes • Query generations

    for transient versioning • Xcode automatic subclass generation • Finally added missed methods on NSManagedObject open class func entity() -> NSEntityDescription open class func fetchRequest() -> NSFetchRequest<NSFetchRequestResult> public convenience init(context moc: NSManagedObjectContext)
  23. What’s New • NSPersistentContainer • Concurrency changes • Query generations

    for transient versioning • Xcode automatic subclass generation • Finally added missed methods on NSManagedObject and • Fetch convenience methods
  24. What’s New func findAnimals() { context.performAndWait({ let request : NSFetchRequest<Animal>

    = Animal.fetchRequest do { let searchResults = try request.execute() // Use search results... } catch { print("Error with request: \(error)") } }) }
  25. Stack Setup

  26. Stack Setup NSManagedObjectModel

  27. Stack Setup NSManagedObjectModel NSPersistentStoreCoordinator

  28. Stack Setup NSManagedObjectModel NSPersistentStoreCoordinator NSManagedObjectContext

  29. Stack Setup

  30. Stack Setup Save Context

  31. Stack Setup Main Context Save Context Parent/Child

  32. Stack Setup Main Context Save Context Parent/Child

  33. Stack Setup Main Context Save Context Parent/Child Edit Context Parent/Child

    Merge changes
  34. Stack Setup NSManagedObjectModel NSPersistentStoreCoordinator NSManagedObjectContext

  35. Stack Setup NSManagedObjectModel NSPersistentStoreCoordinator NSManagedObjectContext NSPersistentContainer

  36. Stack Setup open var viewContext: NSManagedObjectContext { get } open

    func newBackgroundContext() -> NSManagedObjectContext open func performBackgroundTask(_ block: @escaping (NSManagedObjectContext) -> Swift.Void)
  37. Demo

  38. Debugging & Testing

  39. Debugging & Testing • Multithreading assertions

  40. Debugging & Testing • Multithreading assertions

  41. Debugging & Testing • Multithreading assertions

  42. Debugging & Testing • Multithreading assertions • SQL Debug

  43. Debugging & Testing • Multithreading assertions • SQL Debug

  44. Debugging & Testing • Multithreading assertions • SQL Debug 2012-01-19

    20:31:37.864 ToDoSync[3455:fb03] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCOMPLETE, t0.ZCREATEDAT, t0.ZNOTE, t0.ZTITLE FROM ZTASK t0 ORDER BY t0.ZCREATEDAT LIMIT 1 2012-01-19 20:31:37.875 ToDoSync[3455:fb03] CoreData: annotation: sql connection fetch time: 0.0742s 2012-01-19 20:31:37.875 ToDoSync[3455:fb03] CoreData: annotation: total fetch execution time: 0.0784s for 1 rows.
  45. Debugging & Testing • Multithreading assertions • SQL Debug •

    Migration debug -com.apple.CoreData.MigrationDebug
  46. Debugging & Testing • Multithreading assertions • SQL Debug •

    Migration debug
  47. Debugging & Testing • Multithreading assertions • SQL Debug •

    Migration debug • In-memory store
  48. Migration

  49. Migration • Lightweight

  50. Migration • Lightweight • Heavyweight

  51. Migration • Lightweight • Heavyweight

  52. Tips please…

  53. Tips please… #1 Stay up-to-date with your tools

  54. Tips please… #1 Stay up-to-date with your tools #2 Adapt

    changes, be like water…
  55. Tips please… #1 Stay up-to-date with your tools #2 Adapt

    changes, be like water… #3 Be bold, don’t be afraid of making mistakes
  56. Tips please… #1 Stay up-to-date with your tools #2 Adapt

    changes, be like water… #3 Be bold, don’t be afraid of making mistakes #4 Do not judge based on someone’s opinion
  57. Where to go next? WWDC 16 Session 242 What's New

    In Core Data