2

I tried a few things but couldn't find a solution. I am navigating the controller in the onNext block but the deinit is not calling. I have also declared the self as weak in the rx closures.

Below is the complete class code. Please correct if I am doing anything wrong:

class LoginVC: UIViewController {

    //MARK:- Variables & Consts
    private let tag = "LoginVC"
    private let loginViewModel = LoginViewModel()
    private let disposeBag = DisposeBag()
    
    //MARK:- IBOutlets
    @IBOutlet weak var tfUsername: MFTextField!
    @IBOutlet weak var tfPassword: MFTextField!
    @IBOutlet weak var btnLogin: UIButton!
    
    //MARK:- View Controller Life Cycle
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupBindings()
    }
    
    ///deinit
    deinit {
        print(" deinit")
    }
    
    //MARK:- Custom Methods
    
    private func setupBindings() {
        loginViewModel
            .loginResponse
            .observeOn(MainScheduler.instance)
            .subscribe(onNext: { [weak self] loginResponse in
             let vc = UIStoryboard(name: Constants.MAIN, bundle: nil).instantiateViewController(withIdentifier: Constants.HOME_VC)
                self?.navigationController?.pushViewController(vc, animated: true)
            }, onError: { [weak self] error in
                self?.removeLoader()
            }, onCompleted: {  [weak self]  in
                self?.removeLoader()
            }, onDisposed: { [weak self] in
                print("\(self?.tag ?? "") onDisposed")
            })
        .disposed(by: disposeBag)
        
        btnLogin.rx.tap.bind{ [weak self] in
            self?.loginViewModel.login(username: self?.tfUsername.text ?? "", password: self?.tfPassword.text ?? "")
        }.disposed(by: disposeBag)
    }
}

Any help would be appreciated.

Ale
  • 25
  • 8
  • Does this answer your question? [Deinit not called on a UIViewController, but Dealloc is](https://stackoverflow.com/questions/33112147/deinit-not-called-on-a-uiviewcontroller-but-dealloc-is) – bseh Nov 13 '20 at 11:11
  • @bseh the link you provided is completely irrelevant Thanks for sharing thoughts. – Ale Nov 13 '20 at 11:22
  • The code you shared doesn't have a view controller in it. – Daniel T. Nov 13 '20 at 12:16
  • when do you expect this to be called? Are you correctly dismissing this view controller? – Rukshan Nov 13 '20 at 18:45
  • @RukshanMarapana I have updated the code pls check the .subscribe block where I am navigating it to the next controller and the results are still the same. – Ale Nov 14 '20 at 08:23
  • @Ale I answered. – Rukshan Nov 14 '20 at 16:51

2 Answers2

2

You are pushing the next view controller here.

self?.navigationController?.pushViewController

When you do this, LoginVC never gets deallocated because it's still in the memory and navigation controller's stack. So there's no reason for deinit to be called.

deinit will only get called if you dismiss LoginVC. And if your LoginVC is your root viewcontroller it will never call it's deinit. This has nothing to do with RxSwift.

Rukshan
  • 7,902
  • 6
  • 43
  • 61
  • 1
    Thanks, @Rukshan Marapana my problem was similar to this and it has been resolved now :) – Ale Nov 16 '20 at 11:09
0

You are strongly capturing self in your observable chain. Then putting the disposable that holds the subscription into a disposeBag that is a property of self.

This means that you have a reference cycle. Your view controller has captured itself in itself.

Daniel T.
  • 32,821
  • 6
  • 50
  • 72
  • I am declaring as [weak self] in our observable chain. What else I need to do to fix this issue? – Ale Nov 13 '20 at 13:27
  • You aren't declaring as `[weak self]` in every place in the code above. If you have changed the code and are still having a problem, edit the code in the question. – Daniel T. Nov 13 '20 at 13:29
  • @Ale more specifically your `onCompleted` doesn't have a [weak self] in the code you posted above. – Simon McLoughlin Nov 13 '20 at 14:39
  • @DanielT. I have tried the change you asked for and also updated the above code patch but the results are still the same. It's not working. – Ale Nov 13 '20 at 16:54
  • Please post the entire class. Likely there is a retain cycle in code that you haven't posted. – Daniel T. Nov 13 '20 at 16:59
  • @DanielT. I have added the complete class please review the above code. – Ale Nov 13 '20 at 18:29
  • The LoginVC deinit got called using the code you have now provided. The problem is not in the code you presented so far. – Daniel T. Nov 14 '20 at 13:01
  • @DanielT. sounds strange. I have been trying so far but unable to achieve this. – Ale Nov 14 '20 at 16:21
  • @Ale Post a minimum, compilable, example of code that causes the problem you are having. You haven't posted the code that contains the problem yet. – Daniel T. Nov 15 '20 at 17:52