Dhh
Dhh

Reputation: 13

How can i remove “addDisposableTo” when i used RxSwift

when i use RxSwift, i must write many many addDisposableTo, how can i remove it?

usernameTextField.rx.text.orEmpty
            .bindTo(viewModel.username)
            .addDisposableTo(disposeBag)

    passwordTextField.rx.text.orEmpty
        .bindTo(viewModel.password)
        .addDisposableTo(disposeBag)

    repeatPasswordTextField.rx.text.orEmpty
        .bindTo(viewModel.repeatPassword)
        .addDisposableTo(disposeBag)

Upvotes: 0

Views: 408

Answers (2)

Herakleis
Herakleis

Reputation: 533

As pointed out by Nimble, you have to do it; otherwise you will most certainly leak memory. The subscribe/unsubscribe couple at work relies on the same principle as native Swift Notification Center/KVO observers: if you open a "channel", you have to "close" it.

The middle ground here is to use NSObject-Rx (by Ash Furrow) that prevents you from creating a dispose bag everywhere you import RxSwift in an NSObject subclass: simply call .disposed(by: rx.disposeBag).

Upvotes: 1

vasantpatel
vasantpatel

Reputation: 381

I have created a code snippet in XCode for the subscription code and so its very handy that I don't have to type it in every time

I'm afraid since the dispose bag is a property that you tie to the object that is subscribing there is not much you can save in terms of boiler plate code.

May be you could write a convinience wrapper but still is it worth it? In my opinion XCode snippets works just fine

Upvotes: 1

Related Questions