@Environment(\.dismiss) var dismiss - Infinite loop bug.

The following code works:

struct SelectBatteryChemistryView: View {
    @ObservedObject var model: SelectBatteryChemistryViewModel = .init()
    @Environment(\.presentationMode) private var presentationMode

    var body: some View {
        VStack {
            Spacer()
            TitleText("BATTERY MODE")
            SimpleModeSelectorView(
                selected: $model.batteryChemistry
            )
            .pickerStyle(.inline)
            .frame(minHeight: 300)

            PrimaryTitleButton(title: "SELECT") {
                if model.batteryChemistry == .user {
                    model.customChemistryFlow = true
                } else {
                    model.confirmBatterySettings()
                }
            }

            NavigationLink(
                destination: CustomBatteryChemistryView(),
                isActive: $model.customChemistryFlow
            ) { EmptyView() }
                .isDetailLink(false)
        }
        .onReceive(model.$batterySettingsConfirmed) { batterySettingsConfirmed in
            if batterySettingsConfirmed {
                //dismiss()

            }
        }
        .onAppear() {
            model.customChemistryFlow = false
        }
    }
}

The following code causes an infinite loop in the CustomBatteryChemistryView() initialiser.

struct SelectBatteryChemistryView: View {
    @ObservedObject var model: SelectBatteryChemistryViewModel = .init()
    @Environment(\.dismiss) private var dismiss

    var body: some View {
        VStack {
            Spacer()
            TitleText("BATTERY MODE")
            SimpleModeSelectorView(
                selected: $model.batteryChemistry
            )
            .pickerStyle(.inline)
            .frame(minHeight: 300)

            PrimaryTitleButton(title: "SELECT") {
                if model.batteryChemistry == .user {
                    model.customChemistryFlow = true
                } else {
                    model.confirmBatterySettings()
                }
            }

            NavigationLink(
                destination: CustomBatteryChemistryView(),
                isActive: $model.customChemistryFlow
            ) { EmptyView() }
                .isDetailLink(false)
        }
        .onReceive(model.$batterySettingsConfirmed) { batterySettingsConfirmed in
            if batterySettingsConfirmed {
                //dismiss()

            }
        }
        .onAppear() {
            model.customChemistryFlow = false
        }
    }
}

Adding the @Environment(.dismiss) object prevents the use of a navigation link. The program sits there with the stack infinitely calling the getter for the NavigationLink.

It looks like an infinite loop on the accessor due to some strangeness related to the magic of the Dismiss environment object.

How do we pass this on to the Dev team at Apple?

I am having the same or a very similar issue.

same

Same here.

@Environment(.dismiss) and NavigationLink don't work together.

For me, this issue is present since iOS 17, everything was fine till then. Did Anyone find a solution or workaround?

So, we have decided to completely switch over to NavigationPath and navigationDestination, which works perfectly with Navigation. However, we are now finding navigation path has bugs on iOS 17 BETA... So yeah...

dont use it ever, it loops like crazy. Just use UIKit to dismiss, thats what I do

Weirdly enough, for me it happened because I was using an array of tuples inside my View. Did you define an array of tuples anywhere?

You don't even have to use the array (not that you would ever do that), but just having it in the View was enough to cause the infinite loop:

struct SomeView: View {
  ...
  var sections: [(String, String)] = [
    ("String 1", "value 1"),
    ("String 2", "value 2"),
    ("String 3", "value 3"),
    ("String 4", "value 4")
  ]

  var body: some View {
    ...
  }
}

In my case it, was in conjunction with the @Query property wrapper. The query was filling a list. I removed the query, filled it with a bunch a numbers and it didn't have an issue. I think it has something to do with reloading.

@Environment(\.dismiss) var dismiss - Infinite loop bug.
 
 
Q