Post

Replies

Boosts

Views

Activity

Reply to SwiftData: Is syncing changes across ModelContext’s working in Beta 1?
I experienced the same issue and filed FB12319007. You can reproduce the issue by generating a SwiftData app in the Xcode 15 beta and making the following changes: diff --git a/UpdateFromDifferentActorNotSyncing/ContentView.swift b/UpdateFromDifferentActorNotSyncing/ContentView.swift index e508dc8..6c079d0 100644 --- a/UpdateFromDifferentActorNotSyncing/ContentView.swift +++ b/UpdateFromDifferentActorNotSyncing/ContentView.swift @@ -8,6 +8,23 @@ import SwiftUI import SwiftData +// Model actor to add items +actor ItemAdder: ModelActor { + // Conformance to ModelActor + let executor: any ModelExecutor + + init(container: ModelContainer) { + self.executor = DefaultModelExecutor(context: ModelContext(container)) + } + + // Calling this function does not update the items in ContentView + func addItem() { + let newItem = Item(timestamp: Date()) + context.insert(newItem) + try! context.save() + } +} + struct ContentView: View { @Environment(\.modelContext) private var modelContext @Query private var items: [Item] @@ -41,10 +58,13 @@ struct ContentView: View { } private func addItem() { - withAnimation { - let newItem = Item(timestamp: Date()) - modelContext.insert(newItem) - } + // Get our container so we can pass it to the task + let container = modelContext.container + + Task { + let adder = ItemAdder(container: container) + await adder.addItem() + } } private func deleteItems(offsets: IndexSet) {
Jun ’23