Weak reference to delegate/coordinator is lost on iOS 12 and 11, works on iOS 13/14. Swift 5

570 views Asked by At

I am having a weird issue with weak reference. I am using a coordinator pattern and child VCs are communicating with coordinator through delegates. Whenever I am pushing a new VC to navigation stack, I set vc's delegate to conforming coordinator. On iOS 13/14 everything works perfect, but when testing on iOS 12 device, the reference is lost. When removing 'weak' from delegate variable, everything works, but this solution is not optimal.

I use swift 5 and I really hope it won't be an issue. Please help ;(

1

There are 1 answers

0
Charles Pinesky On

Problem fixed, I didn't keep the reference to my main coordinator which led to this problem. On iOS 13 in SceneDelegate it was kept, I forgot to do the same in App Delegate

var window: UIWindow?
var mainCoordinator: MainCoordinator?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    application.accessibilityLanguage = "pl-PL"
    if #available(iOS 13, *) {
        
    } else {
        window = UIWindow(frame: UIScreen.main.bounds)
        let navigationController = UINavigationController()
        mainCoordinator = MainCoordinator(navigationController)
        mainCoordinator?.start()
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()
        
    }
    return true
}