Reactive Programming on Android with RxJava



Download 1,47 Mb.
Pdf ko'rish
bet50/60
Sana20.04.2022
Hajmi1,47 Mb.
#566724
1   ...   46   47   48   49   50   51   52   53   ...   60
Bog'liq
reactiveandroid

protected
void
onDestroy
() {
11
if
(!
disposables
.
isDisposed
()) {
12
disposables
.
dispose
();
13
}
14
}
15
}
In general, unsubscribing from
Disposable
s in an Activity/Fragment
.onDestroy()
method is a
good guideline however, ideally unsubscribing should be done as soon as a resource is no longer
needed. For example, say an
Observable
’s events is only needed when an
Activity
is in the
foreground. In this case, subscription should occur on the
Activity
’s
.onResume()
method and
unsubscription should occur in the opposing lifecycle method–
.onPause()
.


Chapter 5: Reactive Modeling on Android
77
RxLifecycle
Alternatively, an open source library called
RxLifecycle¹⁴
, created by Trello, can be used to
automatically unsubscribe from an
Observable
once an Activity/Fragment lifecycle event occurs.
This way, you don’t have to manage the
Disposable
object explicitly, RxLifecycle will handle it for
you.
To bind a
Disposable
with RxLifecycle, you can specify a lifecycle event when an
Observable
should
be unsubscribed (e.g. on an Activity’s
.onDestroy()
):
1
observable
2
.
compose
(
RxLifecycle
.
bindUntilEvent
(
3
lifecycle
,
4
ActivityEvent
.
DESTROY
5
))
6
.
subscribe
();
Or you can let RxLifecycle decide when it should unsubscribe from an
Observable
. In this case,
it will unsubscribe at the opposing lifecycle event from when the
Observable
was subscribed (i.e.
.onCreate()
/
.onDestroy()
,
.onStart()
/
.onStop()
or
.onResume()
/
.onPause()
).
1
observable
2
.
compose
(
3
RxLifecycleAndroid
.
bindActivity
(
lifecycle
)
4
)
5
.
subscribe
();
In both examples,
lifecycle
represents an
Observable
which can be obtained by
subclassing
RxActivity
/
RxFragment
:
1

Download 1,47 Mb.

Do'stlaringiz bilan baham:
1   ...   46   47   48   49   50   51   52   53   ...   60




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish