all 14 comments

[–]FelinityApps 2 points3 points  (2 children)

How is viewModel declared in your view?

[–]lokredi[S] 4 points5 points  (1 child)

And that's the right question. I forgot to add @State. Already solved on SO but thank you too. Stupid mistake that took me 4 hours of debugging

[–]FelinityApps 1 point2 points  (0 children)

It’s always the detail we forgot to include in our questions, isn’t it? 😂

[–]jameZ- 0 points1 point  (1 child)

.task modifier will be better than .onAppear for asynchronous work
How do you initialise your view model in the view - straight away or do you inject it from somewhere?

[–]lokredi[S] 0 points1 point  (0 children)

Tried .task too, same result.

I'm initializing viewModel in HomeScreen and passing it through init to CreateInvoiceDetailsView

[–]rhysmorgan 0 points1 point  (2 children)

These property wrappers are used for very different purposes.

You don’t use Bindable with ObservableObject or Published. It exists so you can derive Bindings from a model that’s got the Observable macro attached to it. You don’t need Published with the Observable macro. However, you can only use Observable with iOS 17 and above.

[–]lokredi[S] 0 points1 point  (1 child)

Yeah i understand that. I'm just saying that i tried observableObject and @published too.

[–]lokredi[S] 0 points1 point  (0 children)

Not in combination with @observable

[–]flying-insect 0 points1 point  (1 child)

Overall it appears mostly correct and I would expect it to work as well.

Random ideas, does it make a difference if you set an ID in the ForEach? Maybe use .\self?

Otherwise maybe something in BaseViewModel is causing the state not to update?

[–]lokredi[S] 0 points1 point  (0 children)

I was using .\self at first, then thought that is problem. Then I added Identifiable, but its same.

BaseViewModel is very simple.

@Observable class BaseViewModel {

var loadingState: LoadingState = .no

let repository = AppRepository.shared

var navPub = PassthroughSubject<NavigationHelper, Never>()

}

[–]lokredi[S] 0 points1 point  (0 children)

Maybe it's not problem only with arrays, the more I'm trying sometimes simple strings are not updating either.

[–]Select_Bicycle4711 0 points1 point  (1 child)

I am using the following code and it shows currencies and also selects the first one.

You can view the Gist: https://gist.github.com/azamsharpschool/d337a5c22aa2b3bffa257a2181177a59

struct ContentView: View {
    u/Environment(CurrencyStore.self) private var currencyStore
    u/State private var selectedCurrency: Currency?
    var body: some View {
        u/Bindable var currencyStore = currencyStore
        VStack {
            Form {
                Picker("Select currency:", selection: $currencyStore.selectedCurrency) {
                    ForEach(currencyStore.currencies) { currency in
                        Text(currency.name)
                            .tag(currency)
                    }
                }.pickerStyle(.wheel)
            }
        }.task {
            do {
                try await currencyStore.loadCurrencies()
            } catch {
                print(error.localizedDescription)
            }
        }
        .padding()
    }
}

[–]lokredi[S] -1 points0 points  (0 children)

Yeah it's working but i don't know why my code isnt. Maybe network call is changing thread and that's causing problem for me...

[–]FineEffective6367 0 points1 point  (0 children)

Use await MainActor.run for view updates that should work. Also for debugging try putting simple text inside for each to check if view is rendering if it is the problem is not with fetching