|
I'm using SwiftData with related models. And I have a screen that displays all the related items with one specific parent. This detail view uses both add and edit sheets for all the related items. The problem is when I add an item, the sheet dismisses (good), the information is inserted (good) but the screen doesn't refresh so the user can't see that it worked. If you go back to the list of parent items and come into the detail view, the information is there (screen is refreshed). And the edit sheets use @Bindable so the view updates as the user types. But I can't figure out how to get the details screen to refresh after the add action. Here's the relevant code: MODEL
DETAIL VIEW
ADD VIEW
|
|
How do you fetch data from db? Using @Query or another way? Where is the "main" screen in your workflow? |
|
My main screen is a list of Trips using @Query:
But then the TripRowView which has the link to the details screen just uses a let variable. Is that where it's going wrong?
|
|
This is something I only just realized today when pondering a non-updating view, perhaps this will be helpful: TripDetailView will update if it notices a change in an @State var that it either displays directly or uses to display something else, otherwise it doesn't know it should redraw. Alternatively, you could use .onChange(of: statevar, initial: true) { (oldValue, newValue) in } to make the view sensitive to a change in statevar. |
|
Without full code picture difficult to say. As a simple workaround try to add this mofiider to the view, that seems to be not updated propertly
basically whenever you change and come back to this view it makes it update. Very often it helps. |
|
I'm relatively new to SwiftData, but dare to suggest the following, because I've faced a similar issue the last days. At least I would ask you to give it a try: Add an explicit modelContext.save() after your modelContext.insert() statements. In my case, that was the solution. |
|
I appreciate the suggestions and tried each of them. Since they haven't quite worked, I feel terribly inept at this :(
I created a new much simpler app simply to troubleshoot this problem so the code is simpler and everything (almost) is on one screen. DATA MODEL
LIST THAT LINKS TO DETAIL VIEW
DETAIL VIEW WITH ALL THE CODE TOGETHER
AND THEN THE ADD TASK SHEET -- If this were working properly once the task was inserted, it would update the Project (by inserting a new task into the project.tasks field and therefore change the @State Project on the Detail view. Again, I'm not sure if that's not happening because tasks is optional in the Project model.
I also tried inserting |
|
I figured out a solution. Well, I didn't I started looking through example code and the iTour demo app has related data. First I made the asks not optional on the Project model and that didn't fix it. But then I inserted Thank you everyone for the suggestions! |
SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until September 29th.
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.