Skip to content

Commit

Permalink
added auto-save and improved monitoring (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion authored Oct 5, 2024
1 parent 4f20302 commit 0a2b474
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
6 changes: 3 additions & 3 deletions Sources/DataThespian/BackgroundDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
public func delete(where predicate: Predicate<some PersistentModel>?) async throws {
try await self.database.delete(where: predicate)
}

public func save() async throws { try await self.database.save() }
public func insert(_ closuer: @escaping @Sendable () -> some PersistentModel) async
-> PersistentIdentifier
{ await self.database.insert(closuer) }
Expand Down Expand Up @@ -90,10 +90,10 @@

private var database: any Database { get async { await container.database } }

public convenience init(modelContainer: ModelContainer) {
public convenience init(modelContainer: ModelContainer, autosaveEnabled: Bool = false) {
self.init {
assert(isMainThread: false)
return ModelActorDatabase(modelContainer: modelContainer)
return ModelActorDatabase(modelContainer: modelContainer, autosaveEnabled: autosaveEnabled)
}
}

Expand Down
1 change: 1 addition & 0 deletions Sources/DataThespian/Database.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
public import SwiftData

public protocol Database: Sendable {
func save() async throws
@discardableResult func delete<T: PersistentModel>(
_ modelType: T.Type,
withID id: PersistentIdentifier
Expand Down
4 changes: 4 additions & 0 deletions Sources/DataThespian/DatabaseKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@
public import SwiftUI

fileprivate struct DefaultDatabase: Database {
public func save() async throws {
assertionFailure("No Database Set.")
throw NotImplmentedError.instance
}
func delete(_: (some PersistentModel).Type, withID _: PersistentIdentifier) async -> Bool {
assertionFailure("No Database Set.")
return false
Expand Down
9 changes: 5 additions & 4 deletions Sources/DataThespian/ManagedObjectMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@
assertionFailure(error: error)
return nil
}
self.init(
entityName: managedObject.entity.managedObjectClassName,
persistentIdentifier: persistentIdentifier
)
guard let entityName = managedObject.entity.name else {
assertionFailure("Missing entity name.")
return nil
}
self.init(entityName: entityName, persistentIdentifier: persistentIdentifier)
}
}
#endif
Expand Down
20 changes: 19 additions & 1 deletion Sources/DataThespian/ModelActorDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public import SwiftData

@ModelActor public actor ModelActorDatabase: Database, Loggable {
public actor ModelActorDatabase: Database, Loggable {
public func delete<T: PersistentModel>(_: T.Type, withID id: PersistentIdentifier) async -> Bool
{
guard let model: T = self.modelContext.registeredModel(for: id) else { return false }
Expand Down Expand Up @@ -88,6 +88,24 @@
try block(modelContext)
}
}
public func save() throws {
assert(isMainThread: false)
try self.modelContext.save()
}
public nonisolated let modelExecutor: any SwiftData.ModelExecutor
public nonisolated let modelContainer: SwiftData.ModelContainer
public init(modelContainer: SwiftData.ModelContainer, autosaveEnabled: Bool = false) {
let modelContext = ModelContext(modelContainer)
modelContext.autosaveEnabled = autosaveEnabled
let modelExecutor = DefaultSerialModelExecutor(modelContext: modelContext)
self.init(modelExecutor: modelExecutor, modelContainer: modelContainer)
}
private init(modelExecutor: any ModelExecutor, modelContainer: ModelContainer) {
self.modelExecutor = modelExecutor
self.modelContainer = modelContainer
}
}

extension ModelActorDatabase: SwiftData.ModelActor {}

#endif

0 comments on commit 0a2b474

Please sign in to comment.