Swiftpack.co - Package - avdyushin/CoreDataStorage

CoreData Storage

Tiny CoreData helper class and test utils.

How to use?

Create storage object:

let storage = CoreDataStorage(container: NSPersistentContainer(name: "Model"))

It provides two managed object contexts, Main/UI and background:

storage.mainContext // Automatically merges changes from parent context
storage.privateContext // Created once as new background context

Perform block on background context and save:

let cancellable = storage
    .performInBackgroundAndSave { context in
        // work with background context
    .receive(on: DispatchQueue.main)
        receiveCompletion: { completion in
        // process completion on main queue
        receiveValue: { value in

How to test?

Use CoreDataModel to build ManagedObjectModel which you can provide to NSPersistentContainer:

private static let managedObjectModel = CoreDataModel {
    Entity(name: "Item", className: Item.self) {
        Property(name: "id", type: .UUIDAttributeType)
        Property(name: "name", type: .stringAttributeType, isOptional: true)

Model should be created once. managedObjectModel builds model for the managed object:

private final class Item: NSManagedObject {
    @NSManaged var id: UUID
    @NSManaged var name: String?

Create in memory persistent store:

let container = NSPersistentContainer(name: "Temp", managedObjectModel: managedObjectModel)
let description = NSPersistentStoreDescription()
description.type = NSInMemoryStoreType
description.shouldAddStoreAsynchronously = false
container.persistentStoreDescriptions = [description]

How to add it to Xcode project?

  1. In Xcode select File ⭢ Swift Packages ⭢ Add Package Dependency...
  2. Copy-paste repository URL: https://github.com/avdyushin/CoreDataStorage
  3. Hit Next two times, under Add to Target select your build target.
  4. Hit Finish


Stars: 0


Used By

Total: 0