rundfunk47 / stinsen

Coordinators in SwiftUI. Simple, powerful and elegant.
MIT License
926 stars 98 forks source link

hasRoot / isRoot crashes app #96

Open emartinson opened 2 years ago

emartinson commented 2 years ago

Hi! I've adapted example app to handle deeplink, but app crashes when trying to get current coordinator:

final class AppCoordinator: NavigationCoordinatable {
    var stack: Stinsen.NavigationStack<AppCoordinator>

    @Root var unauthenticated = makeUnauthenticated
    @Root var authenticated = makeAuthenticated

    init() {
        let user = User() // obtaining user from persistent store
        stack = NavigationStack(initial: \AppCoordinator.authenticated, user) // this is also handled by auth state listeners
    }

     func makeUnauthenticated() -> NavigationViewCoordinator<UnauthenticatedCoordinator> {
        NavigationViewCoordinator(UnauthenticatedCoordinator())
    }

    func makeAuthenticated(param user: User) -> NavigationViewCoordinator<MainCoordinator> {
        NavigationViewCoordinator(MainCoordinator(user: user)) // MainCoordinator is a simple navigaion view coordinator
    }

    func handleDynamicLink(_ url: URL) {
        guard let coordinator = self.hasRoot(\.authenticated) else { // <-- Crashes here.  self.isRoot(\.authenticated) causes the same crash
            return
        }
    }
}

Crash in NavigationCoordinatable.swift, line 661, col 21

Thread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value

Printing description of route: <ReferenceWritableKeyPath<AppCoordinator, Transition<AppCoordinator, RootSwitch, User, NavigationViewCoordinator>>: 0x280687280> Printing description of inputItem: nil

((input: App.User, comparator: ())?) inputItem = nil Printing description of self: <AppCoordinator: 0x283d59500>

Screenshot 2022-09-18 at 13 27 54

App and coordination logic works perfectly, but this check on AppCoordinator level causes the crash.

It looks that unwraping var root: NavigationRoot! from NavigationStack (NavigationStack.swift, line 29) causes the crash. What is the reason for this crash and how to solve it?

Stinsen (2.0.7) MacOS 12.6, XCode 14 Release, iOS 15-16

emartinson commented 1 year ago

Hi, any updates here? Maybe there are other ways to check if we're currently presenting this screen? Or mark some routes as exclusive (may be showed only once at a time)