CoreDataStack

public final class CoreDataStack

An instance of CoreDataStack encapsulates the entire Core Data stack. It manages the managed object model, the persistent store coordinator, and managed object contexts.

It is composed of a main context and a background context. These two contexts operate on the main queue and a private background queue, respectively. Both are connected to the persistent store coordinator and data between them is perpetually kept in sync.

Changes to a child context are propagated to its parent context and eventually the persistent store when saving.

Warning

You cannot create a CoreDataStack instance directly. Instead, use a CoreDataStackFactory for initialization.
  • The model for the stack.

    Declaration

    Swift

    public let model: CoreDataModel
  • The main managed object context for the stack, which operates on the main queue. This context is a root level context that is connected to the storeCoordinator. It is kept in sync with backgroundContext.

    Declaration

    Swift

    public let mainContext: NSManagedObjectContext
  • The background managed object context for the stack, which operates on a background queue. This context is a root level context that is connected to the storeCoordinator. It is kept in sync with mainContext.

    Declaration

    Swift

    public let backgroundContext: NSManagedObjectContext
  • The persistent store coordinator for the stack. Both contexts are connected to this coordinator.

    Declaration

    Swift

    public let storeCoordinator: NSPersistentStoreCoordinator
  • Creates a new child context with the specified concurrencyType and mergePolicyType.

    The parent context is either mainContext or backgroundContext dependending on the specified concurrencyType:

    Saving the returned context will propagate changes through the parent context and then to the persistent store.

    Declaration

    Swift

    public func childContext(concurrencyType: NSManagedObjectContextConcurrencyType = .mainQueueConcurrencyType,
                             mergePolicyType: NSMergePolicyType = .mergeByPropertyObjectTrumpMergePolicyType) -> NSManagedObjectContext.ChildContext

    Parameters

    concurrencyType

    The concurrency pattern to use. The default is .MainQueueConcurrencyType.

    mergePolicyType

    The merge policy to use. The default is .MergeByPropertyObjectTrumpMergePolicyType.

    Return Value

    A new child managed object context.

  • Resets the managed object contexts in the stack on their respective threads. Then, if the coordinator is connected to a persistent store, the store will be deleted and recreated on a background thread. The completion closure is executed on the main thread.

    Note

    Removing and re-adding the persistent store is performed on a background queue. For binary and SQLite stores, this will also remove the store from disk.

    Declaration

    Swift

    public func reset(onQueue queue: DispatchQueue = .global(qos: .userInitiated),
                      completion: @escaping (StackResult) -> Void)

    Parameters

    queue

    A background queue on which to reset the stack. The default is a background queue with a user initiated quality of service class.

    completion

    The closure to be called once resetting is complete. This is called on the main queue.