Tiny CoreData helper class and test utils.
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)
.sink(
receiveCompletion: { completion in
// process completion on main queue
},
receiveValue: { value in
//
})
Fetch items in given context type:
let cancellable = storage
.fetch(Item.fetchItems(), inContext: .background)
.receive(on: DispatchQueue.main)
.sink(
receiveCompletion: { completion in
// process completion on main queue
},
receiveValue: { value in
let items: [Item] = value
})
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)
}
}.build()
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]
- In Xcode select File ⭢ Swift Packages ⭢ Add Package Dependency...
- Copy-paste repository URL: https://github.com/avdyushin/CoreDataStorage
- Hit Next two times, under Add to Target select your build target.
- Hit Finish