Resolved: Function with a parameter inside init() not updating onAppear{}

In this post, we will see how to resolve Function with a parameter inside init() not updating onAppear{}


Attempting to call a function within init(). I have a function that takes a parameter which I call in the initializer. I set the parameter to a blank string at first until I pass in the value in another view when it appears.
My issue is the function isn’t updating immediately when the view first appears. My objective is to just have the function run immediately once the view is generated and have the view update immediately (where I display in my view values from the function).
My guess is since I’m passing in the initial blank string during the init(), my function isn’t firing with the updated variable. I don’t want to set the @State to @Binding as I don’t want to have to pass in a value everytime I call the observedObject. Any advice is greatly appreciated.
To summarize my issue, when I call the grabItems recipe when my view first appears, it doesn’t initially get called with the correct parameter (it gets called with a blank string).
FYI – pasting my actual grabItems recipe below just as reference in case it helps understand the issue.

Best Answer:

Try this example code to update the View when it first appears.

If you have better answer, please add a comment about this, thank you!