Swiftpack.co -  abdalaliii/CoreDataUtils as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
A set of helper methods for CoreData
.package(url: "https://github.com/abdalaliii/CoreDataUtils.git", from: "0.1")


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 partial implementation for 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
static func findOrCreate(in context: NSManagedObjectContext, matching predicate: NSPredicate, configure: (Self) -> ()) -> Self

If a match isn't found, a new instance will be created and initialized with the provided attribute configuration.


NSManagedObjectObserver class registers for the objects-did-change notification (.NSManagedObjectContextObjectsDidChange). 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: "https://github.com/abdalaliii/CoreDataUtils.git")

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.


Stars: 0
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

32 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API