@@ -124,7 +124,7 @@ final class MainViewController: UISplitViewController {
124
124
let navigationController = UINavigationController ( )
125
125
let tagFilterController = ( viewControllers. first as? MasterContainerViewController ) ? . bottomController as? ItemsTagFilterDelegate
126
126
127
- let coordinator = DetailCoordinator (
127
+ let newDetailCoordinator = DetailCoordinator (
128
128
libraryId: libraryId,
129
129
collection: collection,
130
130
searchItemKeys: searchItemKeys,
@@ -133,8 +133,18 @@ final class MainViewController: UISplitViewController {
133
133
sessionIdentifier: sessionIdentifier,
134
134
controllers: controllers
135
135
)
136
- coordinator. start ( animated: false )
137
- detailCoordinator = coordinator
136
+ newDetailCoordinator. start ( animated: false )
137
+ if let detailCoordinator, presentedViewController != nil {
138
+ // Detail coordinator is about to change while there is a presented view controller.
139
+ // This can happen if e.g. a URL opens an item, and while it is being presented, the collection changes underneath.
140
+ // Existing children are moved to the new instance, so they are properly retained by the new parent coordinator.
141
+ detailCoordinator. childCoordinators. forEach {
142
+ $0. parentCoordinator = newDetailCoordinator
143
+ newDetailCoordinator. childCoordinators. append ( $0)
144
+ }
145
+ detailCoordinator. childCoordinators = [ ]
146
+ }
147
+ detailCoordinator = newDetailCoordinator
138
148
139
149
showDetailViewController ( navigationController, sender: nil )
140
150
}
0 commit comments