Sign the macOS app and keep its state in SwiftData with iCloud sync
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class ServerRecord {
|
||||
var origin = ""
|
||||
var saved = Date.distantPast
|
||||
|
||||
init(origin: String) {
|
||||
self.origin = origin
|
||||
saved = Date()
|
||||
}
|
||||
}
|
||||
|
||||
@Model
|
||||
final class DraftRecord {
|
||||
var scope = ""
|
||||
var json = Data()
|
||||
var saved = Date.distantPast
|
||||
|
||||
init(scope: String, json: Data) {
|
||||
self.scope = scope
|
||||
self.json = json
|
||||
saved = Date()
|
||||
}
|
||||
}
|
||||
|
||||
@Model
|
||||
final class VisitRecord {
|
||||
var taskId = ""
|
||||
var at = 0.0
|
||||
|
||||
init(taskId: String, at: Double) {
|
||||
self.taskId = taskId
|
||||
self.at = at
|
||||
}
|
||||
}
|
||||
|
||||
private let container: ModelContainer = {
|
||||
let schema = Schema([ServerRecord.self, DraftRecord.self, VisitRecord.self])
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: ModelConfiguration(schema: schema, cloudKitDatabase: .private("iCloud.run.fc.byre.mac")))
|
||||
} catch {
|
||||
fatalError("open persistent store: \(error)")
|
||||
}
|
||||
}()
|
||||
|
||||
@MainActor
|
||||
var persistence: ModelContext {
|
||||
container.mainContext
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func fetchRecords<T: PersistentModel>(_ descriptor: FetchDescriptor<T>) -> [T] {
|
||||
do {
|
||||
return try persistence.fetch(descriptor)
|
||||
} catch {
|
||||
log.error("fetch \(String(describing: T.self), privacy: .public): \(String(describing: error), privacy: .public)")
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func saveRecords() {
|
||||
do {
|
||||
try persistence.save()
|
||||
} catch {
|
||||
log.error("save records: \(String(describing: error), privacy: .public)")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user