core-data

Topics related to core-data:

Getting started with core-data

Core Data is a framework in Apple’s various OS SDK including, but not limited to iOS and OS X. It has two major roles a model layer and a persistence layer. The model layer is used in the management of model objects and persist data. Simply you can store and manage data in an object-oriented interface. Primary features include filtering, querying, sorting, persisting data and creating relationships between data. Other subjects of interest to Core Data projects are NSPredicate, threading, and among others.

An example application of Core Data could a Catalog app for your local library. In the Catalog app a librarian could add or remove books. They could also filter books by genre, sort books by publication date, or search for a specific authors work. An entity “Book” would have various attributes such as title, author, publication date, isbn, call number, etc. Core Data including the above example can also store data gathered from a server.

Major components of the framework include:

  • Data Models (entities, attributes, and relationships)
  • Core Data Stack (NSPersistentStoreCoordinator,NSManagedObjectModel, NSManagedObjectContext)
  • NSFetchRequest
  • NSFetchedResultsController

Sources:

Framework Documentation

Programming Guide

Core Data Release Notes 2016

CoreData & Concurrency

It's important to remember that CoreData is NOT thread-safe, which means that if it's necessary to use for example a background-thread to work on ManagedObjects, there are new things to consider, like PrivateQueue- / MainQueue-ManagedObjectContexts.

From Apples documentary: Core Data expects to be run on a single thread. You should never share managed object contexts between threads. This is a hard rule you should not break.

Core Data Stack

This is an implementation of the Core Data Stack which is initially placed in the AppDelegate file if the project is created with Core Data when project is created. These functions can also implemented in separate class for CoreDataStack.swift. One of the major functions is to get the NSManagedObjectContext.

Objective-C

- (NSManagedObjectContext *)managedObjectContext {...}

Swift 2

lazy var managedObjectContext: NSManagedObjectContext = {...}

Swift 3

lazy var persistentContainer: NSPersistentContainer = {...)
let managedObjectContext = persistentContainer.viewContext

The Core Data stack that communicates between the objects in your application and external data stores. The Core Data stack handles all of the interactions with the external data stores so that your application can focus on its business logic. The stack consists of three primary objects: the managed object context (NSManagedObjectContext), the persistent store coordinator (NSPersistentStoreCoordinator), and the managed object model (NSManagedObjectModel).

NSManagedObjectModel

The NSManagedObjectModel instance describes the data that is going to be accessed by the Core Data stack. NSManagedObjectModel (often referred to as the “mom”) is loaded into memory as the first step in the creation of the stack. An example of the NSManagedObjectModel is DataModel.momd. The NSManagedObjectModel defines the structure of the data

NSPersistentStoreCoordinator

The NSPersistentStoreCoordinator realizes objects from the data in the persistent store and passes those objects off to the requesting NSManagedObjectContext. It creates new instances of the entities in the model, and it retrieves existing instances from a persistent store (NSPersistentStore). The NSPersistentStoreCoordinator also verifies that the data is in a consistent state that matches the definitions in the NSManagedObjectModel.

NSManagedObjectContext

When you fetch objects from a persistent store, you bring temporary copies onto the scratch pad where they form an object graph (or a collection of object graphs). You can then modify those objects, unless you actually save those changes, however, the persistent store remains unaltered.

All managed objects must be registered with a managed object context. You use the context to add objects to the object graph and remove objects from the object graph. The context tracks the changes you make, both to individual objects’ attributes and to the relationships between objects. By tracking changes, the context is able to provide undo and redo support for you. It also ensures that if you change relationships between objects, the integrity of the object graph is maintained.

When you save changes the context ensures that your objects are in a valid state. The changes are written to the persistent store (or stores), new records are added for objects you created, and records are removed for objects you deleted.

Source: Apple Core Data Programming: Initializing the Core Data Stack

Creating an Core Data Model

Using Predicates

Sort Descriptors

NSFetchedResultsController