rundfunk47 / stinsen

Coordinators in SwiftUI. Simple, powerful and elegant.
MIT License
822 stars 87 forks source link

StateObject causes crashes with Stinsen #81

Open Kondamon opened 1 year ago

Kondamon commented 1 year ago

We have implemented StateObject into an app, as shown in the example below and we receive a lot of issues with the Coordinator such as crashes. The problems start to arise as soon as the StateObject is changed (due to updates). During debugging I have found out that the RouterStore holds a lot of Coordinators twice or three times and I guess this is the reason for the issues. However, I was not able to yet to create a minimal example for easier debugging of this issue.

struct TestApp: App {
    @StateObject var manager = Manager()

    var body: some Scene {
        WindowGroup {
            NavigationViewCoordinator(
                MainCoordinator()
            )
            .view()
            .environmentObject(manager)
        }
    }
}
onl1ner commented 1 year ago

Hello, @Kondamon!

I'm facing with the exact same issue, after EnvironmentObject updates whenever I try to route to other screen app crashes. Did you find a solution to this problem?

Kondamon commented 1 year ago

@onl1ner, I didn‘t resolve the issue. Used another framework.

onl1ner commented 1 year ago

Oh, I see, @rundfunk47 could you please help with this issue?

rundfunk47 commented 1 year ago

I would recommend against using RouterStore. It will probably be removed in the next version. Instead, I'd recommend you to use whatever dependency injection framework or method you already use throughout your app and put the coordinators there.

Stinsen is not a dependency injection framework and RouterStore comes pretty close to that.

With that being said, I should give it a look 👀

LePips commented 1 year ago

A StateObject should not live at the App level, it will trigger a redraw of the view for new changes and is just generally inadvisable. This is probably throwing the RouterStore into a weird state.

fahmi-dzulqarnain commented 1 year ago

Facing the same issue when trying to implement background scene phase

@Environment(\.scenePhase) private var scenePhase

var body: some Scene {
        WindowGroup {
            MainCoordinator()
                .view()
                .onChange(of: scenePhase) { currentPhase in
                    guard currentPhase == .background else {
                        return
                    }

                    print("App run in background")
                }
        }
}

Is this issue still doesn't have a solution?