deinit not called on child viewcontroller

Viewed 1631

I have a parent viewController that is inserting a childVC with a class method in the childVC. This works great.

class func insertIntoController(parentController: UIViewController, parentView: UIView) -> VideoDoorbellLiveView? {
    guard let liveViewController = parentController.storyboard?.instantiateViewControllerWithIdentifier(liveViewIdentifier) as? VideoDoorbellLiveView else { return nil }

    liveViewController.view.translatesAutoresizingMaskIntoConstraints = false
    parentController.addChildViewController(liveViewController)
    parentView.addSubview(liveViewController.view)

    let views = ["view": liveViewController.view]
    var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views)
    constraints.appendContentsOf(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[view]-0-|", options: NSLayoutFormatOptions.DirectionLeadingToTrailing, metrics: nil, views: views))
    parentView.addConstraints(constraints)

    liveViewController.didMoveToParentViewController(parentController)

    return liveViewController
}

The parent makes sure that a controller is there and sets a couple of String variables, etc. Works beautifully.

    guard let controller = VideoDoorbellLiveView.insertIntoController(self, parentView: liveView) else { return }
    liveViewController = controller // liveViewController is an optional instance variable

My problem is when the parentVC goes away (the deinit is called), the child VC deinit is not called.

Any ideas why? There is no reference of the parentVC in the childVC.

I even tried to do this in the parentVC without guaranteed success:

deinit {
    if let liveViewController = liveViewController {
        liveViewController.removeFromParentViewController()
    }
}
1 Answers

For me I had a retain cycle and needed to pass weak self to firebase observer in parent view controller, then deinit was called on both:

    func observeFeatureChanged(){
    microbeRef.queryOrdered(byChild: Nodes.TIMESTAMP)
        .observe(.childChanged) { [weak self] (dataSnapshot) in
            if let featureDic = dataSnapshot.value as? [String: Any]{
                let featureName = dataSnapshot.key
                if let index = self?.featureNames.firstIndex(of: featureName){
                    self?.featureNames[index] = featureName
                    self?.featureDictionaries[index] = featureDic
                    let indexpath = IndexPath(item: index, section: 0)
                    self?.tableView.reloadRows(at: [indexpath], with: .automatic)
                }
            }
    }
}
Related