I have an app that uses SwiftData which is set to be synced with iCloud.
When I run the app in the simulator and on an actual device simultaneously and then amend the customer name on the simulator the change is not reflected on the device if the records are within a ForEach loop within a List. But if just within a List then it does sync and update automatically.
This updates automatically:
List(customers) { cust in
NavigationLink(cust.name, value: cust)
}
but this doesn't:
List {
ForEach(customers) { customer in
NavigationLink(customer.name, value: customer)
}
I realize that notifications only work one way from the simulator (which is fine for my test) but I am getting back data from iCloud when I make the change so it appears as if iCloud is sending the notification but the UI isn't reflecting this within a ForEach loop.
Inserts and deletions do work when used with both methods above.
So, why does it update correctly within a List but not within a Foreach loop?
List
variant was working correctly as I've never tried it. I was always struggling to get theForEach
variant to update automatically. The solution I've found was to "add a dependency" from theNavigationLink
to thecustomers
query result, by adding:.onChange(of: customers) {}
Perhaps this workaround is working for you too.