From aa669da807e95cbba04deaa1d3d4c692cecbe862 Mon Sep 17 00:00:00 2001 From: Leo Dion Date: Thu, 10 Oct 2024 11:06:36 -0400 Subject: [PATCH] working example --- Example/Sources/ContentView.swift | 27 ++++++++++++------ Example/Sources/DataThespianExampleApp.swift | 30 +++++++++++--------- project.yml | 3 ++ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/Example/Sources/ContentView.swift b/Example/Sources/ContentView.swift index 5c0adc3..7fc7232 100644 --- a/Example/Sources/ContentView.swift +++ b/Example/Sources/ContentView.swift @@ -23,12 +23,19 @@ struct ItemModel : Identifiable { let timestamp: Date } struct ContentView: View { - private let databaseChangePublicist = DatabaseChangePublicist(dbWatcher: DataMonitor.shared) + private static let databaseChangePublicist = DatabaseChangePublicist(dbWatcher: DataMonitor.shared) @State private var items = [ItemModel]() @State private var newItem: AnyCancellable? private static let database = try! BackgroundDatabase(modelContainer: .init(for: Item.self), autosaveEnabled: true) - var body: some View { + fileprivate func updateItems() async throws { + self.items = try await Self.database.withModelContext({ modelContext in + let items = try modelContext.fetch(FetchDescriptor()) + return items.map(ItemModel.init) + }) + } + + var body: some View { NavigationSplitView { List { ForEach(items) { item in @@ -51,14 +58,14 @@ struct ContentView: View { } detail: { Text("Select an item") }.onAppear { - self.newItem = self.databaseChangePublicist(id: "contentView").sink { changes in + self.newItem = Self.databaseChangePublicist(id: "contentView").sink { changes in Task { - self.items = try await Self.database.withModelContext({ modelContext in - let items = try modelContext.fetch(FetchDescriptor()) - return items.map(ItemModel.init) - }) + try await updateItems() } } + Task { + try await updateItems() + } } } @@ -66,7 +73,8 @@ struct ContentView: View { Task { try await Self.database.withModelContext { modelContext in - let newItem = Item(timestamp: Date()) + let timestamp = Date() + let newItem = Item(timestamp: timestamp) modelContext.insert(newItem) try modelContext.save() } @@ -84,6 +92,7 @@ struct ContentView: View { let items : [Item] = models.compactMap{ modelContext.registeredModel(for: $0.persistentIdentifier) } + assert(items.count == offsets.count) for item in items { modelContext.delete(item) } @@ -97,5 +106,5 @@ struct ContentView: View { #Preview { ContentView() - .modelContainer(for: Item.self, inMemory: true) + //.modelContainer(for: Item.self, inMemory: true) } diff --git a/Example/Sources/DataThespianExampleApp.swift b/Example/Sources/DataThespianExampleApp.swift index 558ff14..95cc78a 100644 --- a/Example/Sources/DataThespianExampleApp.swift +++ b/Example/Sources/DataThespianExampleApp.swift @@ -7,26 +7,30 @@ import SwiftUI import SwiftData +import DataThespian @main struct DataThespianExampleApp: App { - var sharedModelContainer: ModelContainer = { - let schema = Schema([ - Item.self, - ]) - let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) - - do { - return try ModelContainer(for: schema, configurations: [modelConfiguration]) - } catch { - fatalError("Could not create ModelContainer: \(error)") - } - }() + init () { + DataMonitor.shared.begin(with: []) + } +// var sharedModelContainer: ModelContainer = { +// let schema = Schema([ +// Item.self, +// ]) +// let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false) +// +// do { +// return try ModelContainer(for: schema, configurations: [modelConfiguration]) +// } catch { +// fatalError("Could not create ModelContainer: \(error)") +// } +// }() var body: some Scene { WindowGroup { ContentView() } - .modelContainer(sharedModelContainer) +// .modelContainer(sharedModelContainer) } } diff --git a/project.yml b/project.yml index ac7778c..c3155e9 100644 --- a/project.yml +++ b/project.yml @@ -21,6 +21,9 @@ targets: sources: - path: "Example/Sources" - path: "Example/Support" + settings: + base: + PRODUCT_BUNDLE_IDENTIFIER: com.demo.DataThespianExample info: path: Example/Support/Info.plist properties: