Dimillian / IceCubesApp

A SwiftUI Mastodon client
https://apps.apple.com/us/app/ice-cubes-for-mastodon/id6444915884
GNU Affero General Public License v3.0
4.88k stars 460 forks source link

Bug/Discussion: ViewModels are initialised multiple times? #2033

Open theedov opened 2 months ago

theedov commented 2 months ago

I'm not sure if this is a bug tbh but I've noticed that @Observable objects are being initialised multiple times. So this @State private var viewModel = TimelineViewModel() for example would call TimelineViewModel.init multiple times.

Here is a forum post I could find about this "issue" https://forums.swift.org/t/observable-init-called-multiple-times-by-state-different-behavior-to-stateobject/70811/8 Apple also suggests initialising objects inside .task which would leave us with optionals everywhere... https://developer.apple.com/documentation/swiftui/state#Store-observable-objects

theedov commented 2 months ago

Another potential option is to conform viewmodel to ObservableObject and keep using @StateObject.

@Observable
final class ViewModel: ObservableObject {
....
}

@StateObject private var viewModel = ViewModel(()
Dimillian commented 2 months ago

FYI, I hear you, and I know the issue is there, but the workaround is just ugly. As I mostly don't do anything in my Observable init I don't really care. This doesn't create an issue as of today for me in this app.

Wouter01 commented 2 months ago

You can fix this by using a custom property wrapper instead of @State: https://hachyderm.io/@groue/112320663017999822

stonko1994 commented 2 months ago

Maybe switching to this approach would help: https://stackoverflow.com/a/71010187/2307466 🤔

theedov commented 2 months ago

FYI, I hear you, and I know the issue is there, but the workaround is just ugly. As I mostly don't do anything in my Observable init I don't really care. This doesn't create an issue as of today for me in this app.

It creates multiple instances of the same VM and doesn't seem to deinitialize all of the "redundant" ones though 🤔

Dimillian commented 2 months ago

Do you see that in the memory graph?

theedov commented 2 months ago

Think so.

image