Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added auto-save and improved monitoring #5

Merged
merged 3 commits into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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