SwiftUi 3.0 - @EnvironmentObject causing NavigationLink to pop

Viewed 167

I've encountered a very odd bug in one of my apps on iOS 15 / SwiftUi 3.0 - can't find any info on it.

I have a series of 3 screens that each link to one another with a tag/selection NavigationLink as below:

NavigationLink(destination: CityListView(city: city, selection: $citySelection, orgId: self.orgId), tag: city.id, selection: $citySelection) {
                CityRow(city: city)
            }

The $citySelection is a binding on each subsequent view to allow the app to programmatically pop the views back to the first when needed.

However, since iOS 15 there is a very odd behaviour when the app is brought foward from the background. Essentially all the views pop back to the first view. Even if I remove all the above bindings it still happens and it seems to be related to one of my @EnvironmentObject.

Each of these views has access to @EnvironmentObject NotificationHandler - notifcation handler is called from many places in the app to let the users know something is happening - background processing / api calls etc. Its very simple - code below:

class NotificationHandler: ObservableObject {

// Boot Notification Center
let nc = NotificationCenter.default

@Published var networkActive: Bool = false

init() {
    print("NOTIFICATION HANDLER BOOTED")
    nc.addObserver(self, selector: #selector(networkStart), name: Notification.Name("networkingStart"), object: nil)
    nc.addObserver(self, selector: #selector(networkStop), name: Notification.Name("networkingStop"), object: nil)
}

@objc private func networkStart() {
    self.networkActive = true
}


@objc private func networkStop() {
    self.networkActive = false  
}

}

Each of my three screens accesses the networkActive variable to decide if it needs to show a progress bar:

if self.notificationHandler.networkActive {
                ProgressBar()
                Spacer()
          }

The problem is, when the app comes back from the background, if the notificationHandler is used at all, the app pops all the screens back to the first one.

If I remove the @EnvironmentObject form the first NavigationLink view this behaviour stops but I obviously can't use the progress bar without it. Accessing the @EnvironmentObject on any of the other views does not cause this behaviour, only the first.

Additionally, this behaviour doesn't happen on iOS 14.5

Any thoughts would be greatly appretiated.

Thanks

0 Answers
Related