I have created a reproduction code.
However, this issue cannot be reproduced with just this code.
In my case, it occurs only in the iOS 16.4 environment when Google AdMob is installed via CocoaPods.
In this case, the 🌟 SubView gets caught in an infinite loop regardless of which SubView is called.
If you comment out the 🌟🌟 "@Environment(.dismiss) private var dismiss" line, it seems to work properly.
Additionally, this code worked correctly on the iOS 16.0 and iOS 16.2 simulators.
I am not sure if the cause of this problem lies in SwiftUI iOS 16.4 or Google AdMob
(or if it's a significant misunderstanding on my part), but I hope for a prompt resolution.
Here is the environment I tested in:
Xcode Version 14.3 (14E222b)
CocoaPods: 1.11.3, 1.12.0
AdMob: 9.14.0, 10.3.0
import SwiftUI
@main
struct iOS16_4NavigationSample2App: App {
var body: some Scene {
WindowGroup {
NavigationStack {
NavigationLink {
ContentView()
} label: {
Text("Content")
}
}
}
}
}
enum Kind { case none, a, b, c }
struct Value: Hashable, Identifiable {
let id: UUID = UUID()
var num: Int
}
@MainActor
class ContentModel: ObservableObject {
@Published var kind: Kind = .a
@Published var vals: [Value] = {
return (1...5).map { Value(num: $0) }
}()
}
struct ContentView: View {
@StateObject private var model = ContentModel()
@State private var selectedData: Value?
// 🌟🌟
@Environment(\.dismiss) private var dismiss
init() {
}
var body: some View {
List(selection: $selectedData) {
ForEach(model.vals) { val in
NavigationLink(value: val) {
Text("1")
}
}
}
.navigationDestination(isPresented: .init(get: {
selectedData != nil
}, set: { val in
if !val {
selectedData = nil
}
}), destination: {
// 🌟
SubView(kind: model.kind)
// SubView()
})
}
}
struct SubView: View {
init(kind: Kind) {
print("init(kind:)")
}
init() {
print("init")
}
var body: some View {
Text("Content")
}
}
# Podfile
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '16.0'
target 'iOS16_4NavigationSample2' do
use_frameworks!
pod 'Google-Mobile-Ads-SDK'
end
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: