ReactiveX / RxSwift

Reactive Programming in Swift
MIT License
24.39k stars 4.17k forks source link

BindSubject to another class always disposed #1433

Closed alfian0 closed 7 years ago

alfian0 commented 7 years ago

I want implement Coordinator pattern with Rx but sometime at another project Observable object won't work,

ViewController

self.button.rx.tap.bind(to: self.viewModel.archieveSelected).addDisposableTo(self.disposeBag)

ViewModel

private (set) var archieveSelected: PublishSubject = PublishSubject()

Coordinator

class OrderCoordinator: NavigationCoordinator {

var navigationController: UINavigationController

var archieveSelected: PublishSubject<Void> = PublishSubject<Void>()

var disposeBag = DisposeBag()

required init(navigationController: UINavigationController) {
    self.navigationController = navigationController

    self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd") // not printed <-----------------------------------
        })
        .addDisposableTo(by: self.disposeBag)

     self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd") // printed <-----------------------------------
        })
}

func start() {
    let viewModel = OrderViewModel()
        viewModel.archieveSelected
            .asObservable()
            .bind(to: self.archieveSelected)
    let viewController = OrderViewController(viewModel: viewModel)
        viewController.tabBarItem.image = #imageLiteral(resourceName: "icon_shop")
        viewController.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_shop_active")
        viewController.tabBarItem.isEnabled = false

    self.navigationController.viewControllers = [viewController]
}
}
alfian0 commented 7 years ago

when i set addDisposableTo will not printed

kzaher commented 7 years ago

Hi @alfian0 ,

why do you think there is a problem with the code in this repo and not some external code?

I want implement Coordinator pattern with Rx but sometime at another project Observable object won't work,

We would need a more detailed description targeted at a specific component to investigate this.

alfian0 commented 7 years ago

@kzaher sorry, solved ...but still confusing, the code running well but we can printed or debug inside subscriber, I give arrow at this code

class OrderCoordinator: NavigationCoordinator {

var navigationController: UINavigationController

var archieveSelected: PublishSubject<Void> = PublishSubject<Void>()

var disposeBag = DisposeBag()

required init(navigationController: UINavigationController) {
    self.navigationController = navigationController

    self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd")  <----------------------------------- // not printed, not paused when set breakpoint
            self.goToSomething() <------------------------------ // will executed and print on function block
        })
        .addDisposableTo(by: self.disposeBag)

     self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd") <----------------------------------- // printed & paused when enable breakpoint
        })
}

func start() {
    let viewModel = OrderViewModel()
        viewModel.archieveSelected
            .asObservable()
            .bind(to: self.archieveSelected)
    let viewController = OrderViewController(viewModel: viewModel)
        viewController.tabBarItem.image = #imageLiteral(resourceName: "icon_shop")
        viewController.tabBarItem.selectedImage = #imageLiteral(resourceName: "icon_shop_active")
        viewController.tabBarItem.isEnabled = false

    self.navigationController.viewControllers = [viewController]
}
  func goToSomething() {
     print("something")  <---------------------------- // printed and paused
  }
}
kzaher commented 7 years ago

Hi @alfian0 ,

there is an obvious difference between those two snippets of code.

self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd")  
            self.goToSomething()
        })
        .addDisposableTo(by: self.disposeBag) <------------------- difference

     self.archieveSelected
        .subscribe(onNext: { (_) in
            print("asd")
        })

Why do you think this is a bug?

kzaher commented 7 years ago

I think you view model probably deallocates and disposeBag terminates the subscription.

kzaher commented 7 years ago

If there is some faulty APIs, then feel free to report another issue, but we can't debug non self contained code examples.