A set of helper methods for CoreData


CoreDataUtils is a framework consisting of several extensions and helpers to take the hassle out of common CoreData tasks.


To use CoreDataUtils, first add import CoreDataUtils" to any classes that will need to use it. CoreDataUtils can be broken down into three basic areas of functionality, as follows.


CoreDataStack is a class for building and managing Core Data stacks, including the model, managed object context, and persistent store coordinator.

Create a new stack
myStack = CoreDataManager(dataModel: "model-name")
myStack.setup() {
Create a background (private queue) context
// This context is a child of the disk writer context and a sibling of the main thread context. 
// Saving it will automatically merge changes into the main context.
let *backgroundContext = myStack.backgroundContext;


Manageable is a protocol on NSManagedObject which provides a partial implementation of helpful methods.

Fetch all objects
static func fetch(in context: NSManagedObjectContext, configurationBlock: (NSFetchRequest<Self>) -> () = { _ in }) -> [Self]
Get the count of objects
static func count(in context: NSManagedObjectContext, configure: (NSFetchRequest<Self>) -> () = { _ in }) -> Int 
Retrieve or create an instance for a new object

You can also find/create objects. If a match isn't found, a new instance will be created and initialized with the provided attribute configurationn.

static func findOrCreate(in context: NSManagedObjectContext, matching predicate: NSPredicate, configure: (Self) -> ()) -> Self


NSManagedObjectObserver class registers for the objects-did-change notification (.NSManagedObjectContextObjectsDidChange). It registers for the context of the managed object we’re interested in, and whenever the notification is sent, it traverses the user info of the notification to check whether or not a deletion of the observed object has occurred

let observer = ManagedObjectObserver(object: mood) { [weak self] type in
    guard type == .delete else { return }


Swift Package Manager:

dependencies: [
    .package(url: "")

Developer Notes

This whole project is a work in progress, a learning exercise and has been released "early" so that it can be built and collaborated on with valuable feedback.


CoreDataUtils is available under the MIT license. See the LICENSE file for more info.


  • Swift Tools 5.3.0


  • None
Last updated: Thu Oct 29 2020 12:42:37 GMT-0500 (GMT-05:00)