Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't switch between presented view controllers #254

Open
2 of 3 tasks
oskarek opened this issue Dec 10, 2024 · 0 comments
Open
2 of 3 tasks

Can't switch between presented view controllers #254

oskarek opened this issue Dec 10, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@oskarek
Copy link
Contributor

oskarek commented Dec 10, 2024

Description

We have noticed what we think is an issue with this library. The problem happens when using the present(item:) method to present a view controller from another, and switch to another one.

Checklist

  • I have determined whether this bug is also reproducible in a vanilla SwiftUI project.
  • If possible, I've reproduced the issue using the main branch of this package.
  • This issue hasn't been addressed in an existing GitHub issue or discussion.

Expected behavior

I expect the new view controller to replace the previously presented one, when the binding driving the navigation is updated to point to the other one.

Actual behavior

Some of the times (it seems to be even most of the times), what instead happens is that the presenting view controller is closed right after the presented one, instead of switching to presenting the new one, along with a console warning about trying to present a view cotroller on top of one whose view is not in the view hierarchy.

Steps to reproduce

  1. Have a view controller Root, that presents another view controller A through a present(item:).
  2. View controller A then in turn presents one of two other view controllers, B and C, depending on the value of a presentation binding.
  3. View controller B is presented, and while it is presented, the binding is set to C instead.
  4. Now, what's expected to happen is that view controller B should be closed and C opened instead. But this only happens some of the times you try it, and at other times it instead just closes both B and A, one ofter the other, going all the way back to Root. And when that happens, there is a warning printed in the console about trying to present view controller C on view controller A whose view is not in the view hierarchy.

This repo reproduces the bug and demos the scenario in which it happens. There is also a fix-branch that uses a fork of this library with a potential fix.

Just open the app in the project, and tap "Present A", then "Present B", then "Present C".

So it seems to be some race condition happening within the navigation machinery. As you can see by looking at the fix in the fork linked above, it seems to be resolved by delaying the dismissal of view controllers with "one tick" by wrapping it in a DispatchQueue.main.async {...}, but I'm not sure it's a good solution.

SwiftUI Navigation version information

2.2.2

Destination operating system

iOS 18

Xcode version information

16.1.0

Swift Compiler version information

swift-driver version: 1.115 Apple Swift version 6.0.2 (swiftlang-6.0.2.1.2 clang-1600.0.26.4)
Target: arm64-apple-macosx15.0
@oskarek oskarek added the bug Something isn't working label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant