Skip to content

Commit

Permalink
Add ModelStorage.insert(_:[ModelData])
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Aug 17, 2023
1 parent 90f88ff commit 55e3bdb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
25 changes: 23 additions & 2 deletions Sources/CoreDataModel/NSManagedObjectContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ extension NSManagedObjectContext: ModelStorage {
try insert(value, model: model)
}

public func insert(_ values: [ModelData]) async throws {
guard let model = self.persistentStoreCoordinator?.managedObjectModel else {
assertionFailure("Missing model")
throw CocoaError(.coreData)
}
try insert(values, model: model)
}

public func delete(_ entity: EntityName, for id: ObjectID) throws {
guard let managedObject = try self.find(entity, for: id) else {
assertionFailure("Object not found for \(id)")
Expand Down Expand Up @@ -75,7 +83,8 @@ internal extension NSManagedObjectContext {

func insert(
_ value: ModelData,
model: NSManagedObjectModel
model: NSManagedObjectModel,
shouldSave: Bool = true
) throws {
// find or create
let managedObject = try find(value.entity, for: value.id) ?? create(value.entity, for: value.id, in: model)
Expand All @@ -87,7 +96,19 @@ internal extension NSManagedObjectContext {
for (key, value) in value.relationships {
try managedObject.setRelationship(value, for: key, in: self)
}
try save()
if shouldSave {
try self.save()
}
}

func insert(
_ values: [ModelData],
model: NSManagedObjectModel
) throws {
for value in values {
try insert(value, model: model, shouldSave: false)
}
try self.save()
}
}

Expand Down
7 changes: 7 additions & 0 deletions Sources/CoreDataModel/NSPersistentContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ extension NSPersistentContainer: ModelStorage {
}
}

public func insert(_ values: [ModelData]) async throws {
let model = self.managedObjectModel
try await performBackgroundTask { context in
try context.insert(values, model: model)
}
}

public func delete(_ entity: EntityName, for id: ObjectID) async throws {
try await performBackgroundTask { context in
try context.delete(entity, for: id)
Expand Down
9 changes: 9 additions & 0 deletions Sources/CoreModel/Store.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public protocol ModelStorage: AnyObject {
/// Create or edit a managed object.
func insert(_ value: ModelData) async throws

/// Create or edit multiple managed objects.
func insert(_ values: [ModelData]) async throws

/// Delete the specified managed object.
func delete(_ entity: EntityName, for id: ObjectID) async throws
}
Expand All @@ -32,6 +35,12 @@ public extension ModelStorage {
func count(_ fetchRequest: FetchRequest) async throws -> UInt {
return try await UInt(fetch(fetchRequest).count)
}

func insert(_ values: [ModelData]) async throws {
for model in values {
try await insert(model)
}
}
}

// MARK: - ModelData
Expand Down

0 comments on commit 55e3bdb

Please sign in to comment.