|
I'm migrating from Core Data and have always used child contexts to enable easy edit / discard of models without resorting to loads of boilerplate assignment between view properties and the model. My models have up to 10 properties and so things get a bit tedious if I follow the example provided by Apple for editing a model in SwiftData so I was delighted to read Paul's suggestion that we use a peer context instead. The difference between my project and the example in the book is that the editing/creating view is presented as a sheet. Interestingly, the presenting view is not updated following an edit which is countrary to what I understand from the way that SwiftData utilises the Observation framework. Specifically, properties in the view that are assigned to a model will force a refresh of the view provided they "touch" an updated property. Model properties that are not displayed by the view will not force an update thus avoiding a discard / rebuild much of the view tree. This is great in principle but it looks like something might be broken here.... I've created a small demo project which illustrates the problem. You can edit the book title or author (both displayed by the presenting view) but the edits are not visible until you return to the list (i.e. you need to manually discard and rebuild the view to get the edits) Model:
App and View code:
There is supposed to be a GIF here but I'm obviously not forming the Sharepoint link correctly...any poiinters would be appreciated! Is this a bug or am I missing something? Regarding a workaround, I thought perhaps the presenting view could use a query (on the passed-in model's identifier, if applicable) but it seems like I might be fighting the system.... |
|
Displaying the details for the selected book using a @Query (constructed in a subview) resolves the issue but does seem clunky to me and will perhaps incur a bit more overhead... Thoughts?
|
|
Hi, Passing the book object to the
|
|
This happened to me too so I've been reloading the view. I assigned a uuid to the id value of the outer view of the code that wasn't refreshing and reset the uuid to a new value when returning. I'd welcome a better solution too. |
SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.