Skip to content

Commit 4784fd1

Browse files
committed
Fix detail coordinator children unintended deinit edge case
1 parent 90a77f6 commit 4784fd1

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Zotero/Controllers/Architecture/Coordinator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ enum SourceView {
1414
}
1515

1616
protocol Coordinator: AnyObject {
17-
var parentCoordinator: Coordinator? { get }
17+
var parentCoordinator: Coordinator? { get set }
1818
var childCoordinators: [Coordinator] { get set }
1919
var navigationController: UINavigationController? { get set }
2020

Zotero/Scenes/Main/Views/MainViewController.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ final class MainViewController: UISplitViewController {
124124
let navigationController = UINavigationController()
125125
let tagFilterController = (viewControllers.first as? MasterContainerViewController)?.bottomController as? ItemsTagFilterDelegate
126126

127-
let coordinator = DetailCoordinator(
127+
let newDetailCoordinator = DetailCoordinator(
128128
libraryId: libraryId,
129129
collection: collection,
130130
searchItemKeys: searchItemKeys,
@@ -133,8 +133,18 @@ final class MainViewController: UISplitViewController {
133133
sessionIdentifier: sessionIdentifier,
134134
controllers: controllers
135135
)
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
138148

139149
showDetailViewController(navigationController, sender: nil)
140150
}

0 commit comments

Comments
 (0)