XCoordinator icon indicating copy to clipboard operation
XCoordinator copied to clipboard

Child Coordinator is deallocated if presented too quickly

Open chefnobody opened this issue 4 years ago • 3 comments

Presenting a child coordinator too quickly sometimes results in the Coordinator getting deallocated. Find the attached sample project as proof.

https://github.com/chefnobody/DeallocSample

In the attached project, you will find NavigationCoordinators that trigger some action after 1.0 second:

RootCoordinator pushes ListCoordinator, which presents ProfileCoordinator, which presents OnboardingCoordinator.

Line ~73 of AppDelegate reads like this:

// Change this line's dispatch time to `.now() + 1.0` to avoid the bug:
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { [weak self] in
   self?.trigger(.profile)
}

Run the code as-is should result in a log like this:

*****
ProfileCoordinator deinit

Changing the dispatch delay time on line 73 to 1.0:

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) { [weak self] in
   self?.trigger(.profile)
}

Does not cause ProfileCoordinator to deallocate and successfully presents the final OnboardingCoordinator

chefnobody avatar May 27 '21 19:05 chefnobody

@chefnobody I am facing the same issue, did you find anyway to avoid it rather than adding a manual delay?

ericklborges avatar Nov 25 '22 16:11 ericklborges

The only think that worked for me was to manually manage the coordinator reference.

var fooCoordinator: FooCoordinator?

func showFoo() -> NavigationTransition {
    let coordinator = FooCoordinator()
    fooCoordinator = coordinator // and latter fooCoordinator = nil
    return .present(coordinator)
}

ericklborges avatar Nov 25 '22 17:11 ericklborges

@ericklborges it's been a long time since I looked at this and no, I didn't solve it w/o the delay. Also, I just stopped using this lib as its not really being maintained.

aaron-anchor avatar Nov 27 '22 21:11 aaron-anchor