|
I've seen several "list not updating" posts on this site. It feels like I've read through all of them and tried to implement the solutions to my code without success. So I figured I'd just ask for myself. Warning: I am very new to this, so I may just be doing something silly. I'm using Core Data with a list of tools Here is my MainViewModel:
In my loan tool view, Ive got a foreach setup to only display tools that have the incart bool as true:
I have a sheet view that I use to select the tool that I want to add to the cart, which takes place in a function add2List in the MainView Model. When the sheet dismisses I want the view to update and show the tool I've selected in the list. It doesn't. If I reload the sceeen it will show, so I know the function is happening, but it's not reloading the view. same problem removing from the list. here is the add2loan sheet view:
The strange thing is I'm doing something very similar in a different set of views, when tools are added to Core Data without issue. Like I said in the beginning, I'm sure it's something simple I have yet to learn in my journey. Any help or guidance would be much appreciated. I've seen some really cool code reading through some forums, but none of those solutions worked for me. Thanks again, Luke |
|
Take a look on my projetc: https://github.com/Sebastien-Remy/hackingWithmacOS-16-Bookworm |
|
i took a glance before i left the house this morning, but didnt have a lot of time. I'll take another look later. Is there a specific part that you think I should pay attention to? Sorry if thats a silly question and its obvious, I'm still learning. |
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! |
|
@lukepilk - change also move |
|
ok, did that stuff, also needed to pass still no dice. takes a screen reload to see the tool added to or removed from the list view on loanTool |
|
if you can post the project , trying it out with the coredata model will be better, if your files are some thing you are comfortable sharing with ... |
|
It sounds like the problem that you are experiencing is that the data model is not being updated on the @MainActor thread. So, when you add a new item to your data model, your View senses that a change has happened, and reloads it's body. But the View finishes reloading its body before the updates to the data model have finished. Then, by the time the data finishes making its changes, the View thinks that it has already reloaded its body since the last change, and doesn't need to do it again. That would explain why the data is there when you reload the view, but it doesn't automatically show if you don't reload the view. I'm not sure exactly what the fix would be, because I am not very confident in my threading/concurrency skills just yet. But, I would suggest looking into that type of thing. It may be as simple as making your ViewModel an |
|
@AmitShrivastava, I'm doing this to learn so it's ok to share my project. it's here: https://github.com/lukepilk/ToolKeeper I've done something now trying to fix the issue that I can no longer update the list with a screen reload. ugh! @Fly0strich, I don't know anything about threading, I'll dig into that. thanks for the hint. |
|
@lukepilk you did a great job , some changes now 1) File ToolKeeperApp - remove 2) ContentView - add 3) go to Toolbox file remove the @StateObject line and instead add - 4) addTool - again remove @StateObject and add 5) loanTool - @ObservedObject var vm: CoreDataViewModel, remove @ObservedObject from preview , add loanTool(vm: CoreDataViewModel()) 6) add2load - change to @ObservedObject var vm: CoreDataViewModel , in preview add2loan(vm: CoreDataViewModel()) Now your code will work, however when you restart , the data is not being saved, that is to do with how you are saving the data, you can try that on your own i guess Hope it helps , good luck |
|
@AmitShrivastava, Thank you so much for taking the time to help me learn through this experience. I did the things you suggested and still cannot get the loan tool screen to update with the tool that I have selected when add2loan sheet closes. There is a very real possibility that I did not do it correctly, but I pushed it up to the github. I'm going to keep working at it. it's helping me learn regardless. TY |
|
@lukepilk - you just have to make sure you use |
|
I downloaded your project and I made the next two changes and now it works as I guess you intented. Change 01 - In your loanTool View
Change 02 - In your loanTool View . Note: Your added2List Array was not saved..before. As you can see I am not using the added2List so you can review your code and remove as required.
|
|
that did the trick. Thank you to all who have been working with me and helping me figure this out. I'm going to keep building on this thing as a learning experiment. I'll keep the code public so if anyone wants to check it out in the future. woot woot! coding is so fun! |
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.