$30 off During Our Annual Pro Sale. View Details »

Core Data Fundamentals

Ajith R Nayak
September 22, 2021

Core Data Fundamentals

I gave a talk on Core Data in a Nutshell and its concurrency rules.

Ajith R Nayak

September 22, 2021
Tweet

More Decks by Ajith R Nayak

Other Decks in Technology

Transcript

  1. Core Data Fundamentals
    - @ajithrnayak

    View Slide

  2. Core Data Vs Developers

    View Slide

  3. Core Data Stack
    NSPersistentStoreCoordinator
    SQLite
    NSPersistentContainer
    NSManagedObjectContext
    NSPersistentStore
    NSManagedObjectModel
    App

    View Slide

  4. NSManagedObjectContext
    • Like a scratchpad


    • fetch, change, add and delete


    • Create more than one contexts


    • Updates persistent store

    View Slide

  5. Concurrency policies
    ConcurrencyType
    NSPrivateQueueConcurrencyType

    (background queue)
    NSMainQueueConcurrencyType

    (application’s main queue)

    View Slide

  6. perform(_:)
    • asynchronous method


    • gets wrapped in a autorelease pool.
    performAndWait(_:)
    • synchronous method


    • will block the calling thread


    • does not wrap the block in an

    autorelease pool.

    View Slide

  7. Summary
    • A managed object context is tied to a serial queue.


    • Create only two contexts for simplicity.


    • Concurrency types are two but contexts can be many.


    • Context executes on a “private” queue.


    • Performance heavy operations should be performed on the background queue.


    • Don’t pass NSManagedObject instances between queues.


    • To avoid unnecessary saves, be aware of automatically triggered broadcasts.

    View Slide

  8. Thank you

    View Slide