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

"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.

CocoaHeads Ukraine

January 03, 2017
Tweet

More Decks by CocoaHeads Ukraine

Other Decks in Programming

Transcript

  1. Agenda • Core Data Introduction • What’s New in Core

    Data • Stack Setup • Demo • Debugging & Testing • Migration • Tips
  2. Introduction • What’s Core Data • Key features • Faulting

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

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

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

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

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

    • Attributes validation • Schema migration • Interface synchronization • Sophisticated queries
  8. What’s New • NSPersistentContainer • Concurrency changes • Query generations

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

    for transient versioning • Xcode automatic subclass generation • Finally added missed methods on NSManagedObject
  10. 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)
  11. 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
  12. 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)") } }) }
  13. Stack Setup open var viewContext: NSManagedObjectContext { get } open

    func newBackgroundContext() -> NSManagedObjectContext open func performBackgroundTask(_ block: @escaping (NSManagedObjectContext) -> Swift.Void)
  14. 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.
  15. Debugging & Testing • Multithreading assertions • SQL Debug •

    Migration debug -com.apple.CoreData.MigrationDebug
  16. 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
  17. 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