Reactive Programming on Android with RxJava



Download 1,47 Mb.
Pdf ko'rish
bet36/60
Sana20.04.2022
Hajmi1,47 Mb.
#566724
1   ...   32   33   34   35   36   37   38   39   ...   60
Bog'liq
reactiveandroid

Schedulers.trampoline()
provides a
Scheduler
that schedules work on the current thread
(i.e. using this
Scheduler
will block the current thread). Scheduled tasks are not executed
immediately and instead are put in a queue and executed after the current unit of work is
completed. This particular
Scheduler
is typically used when implementing recursion to avoid
infinitely growing the call stack.
Observable.subscribeOn()
The
.subscribeOn()
operator is used in the
Observable
chain to dictate where the
Observable
should operate (i.e. the function inside of
.create()
). Rewriting the previous example using a
Scheduler
instead gives us:
1
Observable
<
Integer
>
integerObservable
=
Observable
.
create
(
source
-> {
2
Log
.
d
(
TAG
,
"In subscribe"
);
3
source
.
onNext
(1);
4
source
.
onNext
(2);
5
source
.
onNext
(3);
6
source
.
onComplete
();
7
});
8
Log
.
d
(
TAG
,
"Created Observable"
);
9
10
Log
.
d
(
TAG
,
"Subscribing to Observable"
);
11
integerObservable
.
subscribeOn
(
Schedulers
.
newThread
())
12
.
subscribe
(
i
->
Log
.
e
(
TAG
,
"In onNext(): "
+
i
));
13
14
Log
.
d
(
TAG
,
"Finished"
);


Chapter 4: Multithreading
51
Running the above code is similar to using a
Thread
in that the operations inside
.create()
will
now occur in a separate thread provided by
Schedulers.newThread()
. The benefit of this approach
over using a
Thread
is that tacking on a
Scheduler
to specify where the
Observable
should execute
is
declarative
and we no longer have to worry about the low-level details of how to create and run
a thread. Furthermore, we can decouple the
Observable
creation from the threading; the subscriber
can determine the thread on which to run the
Observable
rather than having that decision baked
into the
Observable
itself.
.subscribeOn()
controls what
Scheduler
is used to execute the statements inside the
.create()
.

Download 1,47 Mb.

Do'stlaringiz bilan baham:
1   ...   32   33   34   35   36   37   38   39   ...   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