RxSwift | Initialize and push UIViewController in closure

211 views Asked by At

For example we have three UIViewControllers: A, B, C.

  1. We pushing B from A.

  2. In B we calling some API:

func getProduct(productNumber: String) {
        someService.rxGetProduct(productNumber: productNumber)
            .asObservable()
            .trackActivity(loading)
            .subscribe(onNext: { [weak self] product in
                guard let `self` = self else { return }
                let cViewModel: CViewModel = .init()
                let cViewController: CViewController = .init(viewModel: cViewModel)
                self.navigationController?.pushViewController(cViewController, animated: true)
            }, onError: { [weak self] error in
                // error handling
            }).disposed(by: disposeBag)
}
  1. In method above, we are getting some product model and pushing C view controller in closure.

The problem is, when we are popping back from C view controller to B view controller – C view controller is not deinitializing. C view controller is deinitializing when we are popping back from B view controller to A view controller.

What I am doing wrong?

1

There are 1 answers

0
MarkHim On
  1. You might be creating the retain cycle within trackactivity or whatever loading is
  2. consider using take(1) and asSingle() in case your observable is not intended to complete.