Skip to content

Instantly share code, notes, and snippets.

@devahmedshendy
Last active January 16, 2024 08:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devahmedshendy/0c13af56a08b2a03303d33ae73ba17fa to your computer and use it in GitHub Desktop.
Save devahmedshendy/0c13af56a08b2a03303d33ae73ba17fa to your computer and use it in GitHub Desktop.
Core Data Fundamentals - CheatSheet (Simple)

Core Data Fundamentals - CheatSheet (Simple)

Here, I will save basic coredata information for anyone/me would like a quick refresh for these fundamentals from time to time.

What is Core Data

  • Core Data is not a database, but instead it manages an Object Graph.

Multithreading

  • NSManagedObject and NSManagedObjectContext in Core Data are not thread-safe.

  • Core Data avoids data race issues by operating on NSManagedObject instances and NSManagedObjectContext instances in a serial queue.
    This is why we need to place the operation code within the closures of perform or performAndWait.

  • Developers should perform operations on the main thread queue for the view context and the managed object instances registered within it. Similarly, we should perform operations on the serial queue created by the private context for private contexts and the managed objects registered within them.

Several Tips on Core Data Concurrency Programming

  • Enable Core Data Concurrency Debugging Parameters
    To void issues like .......... to be continued

Misc

  • ModelContext of SwiftUI is like a wrapped version of NSManagedObjectContext

References:

@devahmedshendy
Copy link
Author

devahmedshendy commented Jan 15, 2024

Overview of CoreData Stack

  • These are the core classes of CoreData: Management Object Model, Managed Object Context, Persistent Store Coordinator
    Screenshot 2024-01-15 at 10 58 22 PM

  • Here is how these classes are related to one way to another:
    Screenshot 2024-01-15 at 10 58 28 PM

@devahmedshendy
Copy link
Author

devahmedshendy commented Jan 15, 2024

Managed Object Model

  • It is instance of NSManagedObjectModel class.
  • It represents the data model of the application.
    Screenshot 2024-01-15 at 10 58 51 PM
  • and it is connected directly to Data Model object.
    Screenshot 2024-01-15 at 11 14 06 PM

@devahmedshendy
Copy link
Author

Data Model

  • It is represented by a file in the application bundle.

  • It contains the data schema of the application.

  • It is nothing more than a collection of entities.
    Screenshot 2024-01-15 at 10 59 18 PM

  • An entity have a collection of attributes, and relationships.

@devahmedshendy
Copy link
Author

devahmedshendy commented Jan 16, 2024

Managed Object Context

  • It is an instance of NSManagedObjectContext class.

  • An app can have one or more managed object context.

  • It is the object you interact with most.

  • It Creates, Reads, Updates, Deletes Managed Objects.

  • Each managed object context manages a collection of Managed Objects.
    Screenshot 2024-01-16 at 5 00 47 AM

  • It receives the Managed Object through a Persistent Store Coordinator
    Screenshot 2024-01-16 at 5 01 38 AM

@devahmedshendy
Copy link
Author

devahmedshendy commented Jan 16, 2024

Persistent Store Coordinator

  • It is an instance of NSPersistentStoreCoordinator class.
  • It is possible to have one or more of it in a single application.
  • Most applications have only one instance of it.
  • It is connected to one or more Persistent Stores.
    Screenshot 2024-01-16 at 5 05 36 AM

What is a Persistent Store

  • Core Data supports 3 types: SQLite, Binary, In-Memory.

@devahmedshendy
Copy link
Author

devahmedshendy commented Jan 16, 2024

How Does Core Data Works

  • The heart of Core Data is the Persistent Store Coordinator.

  • Persistent Store Coordinator is instantiated first when core data is created.

  • Persistent Store Coordinator needs Managed Object Model instance to know what data schema for the application look like.

  • After that, the Managed Object Context is initialized.

  • Now the core data is setup, and application is ready to interact the application Persistent Store through the Managed Object Context.

  • When the changes made to Managed Context Object or saved, it pushes them to the Persistent Store Coordinator which send the changes to the corresponding Persistent Store.

  • When you have multiple Persistent Store, the Persistent Store Coordinator figures out which one is needed to save the changes.

@devahmedshendy
Copy link
Author

NSPersistentContainer Class

  • It encapsulates the details of setting and managing a core data stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment