|
Hi all! Long time listener, first time caller. This is a bit tough to simplify, but I'll try to describe the problem I'm trying to solve. Use CaseMy app has a The CodeWhen the user taps a
The ProblemWhen I make changes to the underlying How can I keep that Ideas
Anyone have any other ideas or suggestions? Thank you! |
|
hi, i'll weigh in on this with support for your question, and with the hope that someone can really answer this, yet i will offer two thoughts (which may or may not provide any comfort). i have often struggled with the problem that you describe. (1) there is a concept of an "ObservableArray," which tacks on an AnyCancellable to every item in the array it holds and then passes any incoming objectWillChange message to be an objectWillChange message from the array to anyone downstream. Combine is not anything i feel comfortable with just yet, although i found something out on Stack Overflow some time ago about this concept of an "ObservableArray." it may require some searching on your part to find it and make sense out of it. (2) i have an app (in a public, on-going state as i struggle with this general notion) that has basically the same structure that you describe. show a list -- go to a detail sheet and make changes -- then return -- and hope the list display updates. sometimes simply adding an @ObservedObject in the right place makes this work out fine; but in other places, everything might just work or not. but for my use case, data comes from Core Data and, therefore, reaps a lot of benefit from the you can find my on-going, fail-in-public project Shopping list on Github. not sure there's an answer here, but i will certainly follow this thread. hope that helps, DMG |
|
If I understood the problem right, you need to annotate
|
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! |
|
Not sure I am understanding correctly, but: Is your viewModel not being refreshed when a change is made to a Task - that should cause the view to refresh and then your Task will no longer be stale in the List? |
|
Thanks for the replies folks. It sounds like I didn't do a create job describing the problem which I expected, it's kind of hard to distill it to a simple question. The Fundamentally, I don't understand how to handle something like this in a declarative form. Ideally, I would want SwiftUI to automatically update my The |
|
Actually, I think I did understand you. I have an app that does a similar thing, and when I checked my code I was passing in the ID of the database row rather than a struct as you are. That's probably because I come from a heavily database-oriented background though, and not because I'm doing the right thing! |
|
If you want to share the |
|
|
|
In your code you create a State variable, shownTask, but never use it - you use viewModel.shownTask instead. Is that correct? |
|
@guseulalio - I do see what you mean in that if it were a class it can be shared, and maybe that is the right way to do it. I hate to lose the value semantics for this kind of thing. Good catch @lordmooch on the @State vs. ViewModel... I was simplifying my example to post here. Technically I do have it in my viewModel, but same thing happens when I had it as @State. I ended up actually doing the same as you - passing the ID into the sheet, and fetching the current Task from my repo. It solves my problem. I made a 2nd initializer that can also just take a struct that I use for my Previews, so that seems to aleviate my concerns fairly well. I still feel slightly dissatisfied that I can't find a good way to keep something updated that was set by the user interaction, but I guess it's sort of the intrinsic nature of declarative style UI which I'm still learning. Thanks for the help everyone. |
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!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.