Skip to content

Commit

Permalink
working example
Browse files Browse the repository at this point in the history
  • Loading branch information
leogdion committed Oct 10, 2024
1 parent 650ef4e commit aa669da
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
27 changes: 18 additions & 9 deletions Example/Sources/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item>())
return items.map(ItemModel.init)
})
}

var body: some View {
NavigationSplitView {
List {
ForEach(items) { item in
Expand All @@ -51,22 +58,23 @@ 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<Item>())
return items.map(ItemModel.init)
})
try await updateItems()
}
}
Task {
try await updateItems()
}
}
}

private func addItem() {

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()
}
Expand All @@ -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)
}
Expand All @@ -97,5 +106,5 @@ struct ContentView: View {

#Preview {
ContentView()
.modelContainer(for: Item.self, inMemory: true)
//.modelContainer(for: Item.self, inMemory: true)
}
30 changes: 17 additions & 13 deletions Example/Sources/DataThespianExampleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit aa669da

Please sign in to comment.