LoginSignup
1
0

More than 1 year has passed since last update.

RxBinding4でよく使うメソッドと注意点

Last updated at Posted at 2020-10-21

RxBindingのドキュメントが非常に少ないので、よく使うメソッドを書き残します。

JakeWharton/RxBinding
https://github.com/JakeWharton/RxBinding

Version

implementation "com.jakewharton.rxbinding4:rxbinding:4.0.0"

View

clicks()

viewがタップされたらObservableを流す。

view.clicks().subscribe{}

longClicks()

viewがロングタップされたらObservableを流す。

view.longClicks().subscribe{}

touches()

viewがタップされたらObservableを流す。

view.touches().subscribe{}

RxBindingを使うときの注意点

RxBindingには、以下のような注意書きが書かれている。

Warning:
The created observable keeps a strong reference to view.
Unsubscribe to free this reference.

つまり、画面を離れるときには、RxBindingに紐づけたViewの参照を切るべきということ。
一方で、issueには以下のように記載されています。
https://github.com/JakeWharton/RxBinding/issues/282

Treat a subscribed RxView.clicks() (or any Observable from this library for that matter) like you would the View reference itself. If you pass it (or subscribe to it) somewhere outside the lifetime of the View, you've just leaked your entire activity.

So if you're just subscribing inside your ViewHolder there's no need to unsubscribe just like there'd be no need to unregister a click listener were you doing it manually.

つまり、該当のViewの外でRxBindingを使用するのであればRxの購読を解除する必要がありますが、Viewの内部で使う限りはとくに購読解除する必要はありません。

1
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0