hmlongco / Factory

A new approach to Container-Based Dependency Injection for Swift and SwiftUI.
MIT License
1.85k stars 116 forks source link

Conflict with Supabase package? #238

Open sderungs99 opened 1 week ago

sderungs99 commented 1 week ago

Hi,

Thank you for this great package! I have one very specific issue, which I am not sure if it is a problem with Factory, Supabase, or with my implementation. This is the scenario:

Package Dependencies:

Swift version: 6.0

Navigation: Main Page -> Navigate to Child View -> Navigate to Child Child View.

Issue: When using a @Environment(\.dismiss) var dismiss in "Child View", the navigation from "Child View" to "Child Child View" crashes the app completely.

Here is a very basic example, with which you can replicate the issue:

import SwiftUI
import Factory

@main
struct BlankFactoryDemoApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

struct ContentView: View {
    var body: some View {
        NavigationStack {
            List {
                NavigationLink {
                    ChildView()
                } label: {
                    Text("Go to Child View")
                }
            }
            .navigationTitle("Factory/Supabase Demo")
        }
    }
}

struct ChildView: View {
    private let demoService = Container.shared.demoService()
    @Environment(\.dismiss) var dismiss

    var body: some View {
        List {
            NavigationLink {
                ChildChildView()
            } label: {
                Text("Go to Child Child View")
            }
        }
        .navigationTitle("Child View")
    }
}

struct ChildChildView: View {
    private let demoService = Container.shared.demoService()

    var body: some View {
        List {
            Text("Child Child View")
        }
        .navigationTitle("Child Child View")
    }
}

class DemoService {
    func doSomething() {
        print("Hello from DemoService")
    }
}

extension Container {
    var demoService: Factory<DemoService> {
        self { DemoService() }
    }
}

The app does not crash if either one of the following is done:

Would be great if someone could check this. Let me know if more details are required to replicate the problem.

Cheers, Sascha

hmlongco commented 4 days ago

There's been some weirdness going on with dismiss recently. See...

https://www.linkedin.com/posts/qazi-ammar-arshad-a24895123_swiftui-iosdev-github-activity-7247929481892421634-1p7q