|
Hi, I'm struggling with the following SwiftUI code in my personal project, I hope someone can help me. Whenever I click one of these buttons, the score of the team gets successfully updated in the database. But the score in this @Bindable is doing a good job it seems, because it correctly updates the score in This applies to both preview as running the app in the simulator: clicking the button doesn't display the updated score on the button
Some code of the ContentView:
These are the models:
UPDATE: if I:
So I must be overlooking something when accepting a SwiftData Model in a child view via @Bindable and writing to that object. I've made a video, hopefully this helps (please remove the spaces from the url to make it work): https ://app.screencast.com/BeD69yrfRsNZu |
|
I found a workaround. It seems that Swiftui shows the updated score IF:
For instance I display the
AND At the moment I update the score, I also reassign the value of this date:
So the score, which is derived from an object related to the @Bindable field, gets only refreshed if I reassign (or update) a property directly on the @Bindable field. So I have this workaround, which is good. But can anyone tell if this behaviour is intended? Or am I perhaps misusing something in SwiftUI? Thanks in advance! |
|
First: Thanks for coming back and sharing your findings. I was on the same track in Playgrounds to test this same assumption. Second: One of my peeves. If you and I were chatting in a pub with a pint, you'd (probably) describe your views by saying,
That's how I'd imagine it. You don't have a Intended Behaviour?(You and I are in agreement here...)
Now for the behaviour part. I think what you're seeing is intended behaviour. You've alterted SwiftUI that you want to bind a But why then, doesn't the score update? Why does the view not redraw itself? What is being observed? It's the Still unsureBut I might also argue that the number of items in the Keep Coding!If I find more info, I'll be back. |
|
Hi Obelix, Thank you very much for coming back to me, and providing me with the feedback. From now on I'll always immediately rename the ContentView! To be clear: the number of items in the participation array doesn't change. I do add an item to the points array when clicking the button. Here's how the relationship looks like:
(I might get rid of the sections between |
|
Hi
I will give you one of your stories. You have a box, you open the box to look at the While some time I might rename the So I do not think of You have mention this a few times and felt that while it might YOUR peeves it not others. People should write code that they are happy to use (while some thing are Swift convention and these are not set in stone!). A bit like which is the better architecture compared to other! Do like your way of look at problems in a different way. |
|
My current workaround consists out of displaying a property of the game on screen, but currently I cannot think of a property on the So currently I'm forced to put a property on screen which doesn't make sense in my case. If anyone has a better workaround, or can suggest me a better way of composing the model, please let me know! |
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.