public final class CoreDataStack
extension CoreDataStack: Equatable
extension CoreDataStack: CustomStringConvertible
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.
WarningYou cannot create a
CoreDataStackinstance directly. Instead, use a
Describes the result type for creating a
public typealias StackResult = Result<CoreDataStack, Error>
The model for the stack.
public let model: CoreDataModel
The persistent store coordinator for the stack. Both contexts are connected to this coordinator.
public let storeCoordinator: NSPersistentStoreCoordinator
Creates a new child context with the specified
backgroundContextas the parent.
mainContextas the parent.
Saving the returned context will propagate changes through the parent context and then to the persistent store.
public func childContext(concurrencyType: NSManagedObjectContextConcurrencyType = .mainQueueConcurrencyType, mergePolicyType: NSMergePolicyType = .mergeByPropertyObjectTrumpMergePolicyType) -> NSManagedObjectContext.ChildContext
The concurrency pattern to use. The default is
The merge policy to use. The default is
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.
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.
public func reset(onQueue queue: DispatchQueue = .global(qos: .userInitiated), completion: @escaping (StackResult) -> Void)
A background queue on which to reset the stack. The default is a background queue with a “user initiated” quality of service class.
The closure to be called once resetting is complete. This is called on the main queue.