|
Hi everyone, I've been trying to work with some dynamic lists and arrays in my plant-care app, and it's been difficult trying to work with ObservableObject to refresh/update views when I'm not in the primary view to update an array. Specifically, what I'm trying to do in View 1 is update the list by going to the New Sheet popup that triggers on the green Add Reminder button. I've also added a blue "add a new reminder" button up top to test that it works if the button is on the same view. However, when you attempt to add a reminder within the New Sheet view, the list does not refresh/update. I've tried experimenting with objectWillChange.send() and other functions that I can embed to get it to change, but alas I'm stuck. Any help would be greatly appreciated! Here's all the code I'm working with, simplified for ease of troubleshooting (I went off the framework found in this thread):
|
|
As an update, I have also tried changing the RemindersClassModel to a struct instead of a class (to try a value-type change instead), but unfortunately the addition of reminders from the pop-up view still isn't working. |
|
Did you try with EnvironmentObject ? In SceneDelegate
In ContentView
In NewSheetTest
|
|
hi, the problem is that your NewSheetTest creates its own RemindersArray with
so your instead, your code for the
and you code in
one other thing: your RemindersArray class does not need to call
hope that helps, DMG |
|
@delawaremathguy I actually saw all the other replies and support you've given on other threads where I've found solutions to similar problems - I was praying that you would respond to this one haha. Thanks so much Jerry, you're a lifesaver!! @achuaswani Thanks for the suggestion! I have limited experience with EnvironmentObject so I will definitely be using this method for other views in my app. |
|
Hey @delawaremathguy, sorry for the bother but was wondering if I could borrow you for one more question - so I've got the above setup working perfectly with your code fix, and I can easily add and delete reminders. Now, I'm trying to add one more layer to it - I have a plant-tracking app, and I can't seem to figure out how to assign reminders to their own specific plant object. Right now, the ContentView from above holds a RemindersArray, with each object in that array being their own unique reminder. Now, I'm trying to introduce a PlantView/Array that holds unique plants, each with its own unique RemindersArray. I initiate both the PlantData and RemindersArray in SceneDelegate as EnvironmentObjects as well. For the NewSheetTest, I'm guessing I should be passing in PlantData somehow? Currently it stands as:
..and NewSheetTest does not contain any reference to the PlantArray object. Any help would be great, I'm so close to cracking it! |
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.