Reactive Programming on Android with RxJava


Observable.fromCallable(Callable



Download 1,47 Mb.
Pdf ko'rish
bet24/60
Sana20.04.2022
Hajmi1,47 Mb.
#566724
1   ...   20   21   22   23   24   25   26   27   ...   60
Bog'liq
reactiveandroid

Observable.fromCallable(Callable callable)
.fromCallable()
creates an
Observable
that when subscribed to, invokes the function supplied,
and then emits the value returned by that function.
Constructing an
Observable
with
.fromCallable()
won’t actually do the work specified inside
the
Callable
. All it does is define the work to be done. As such,
.fromCallable()
is the ideal
construction method to be used for any potentially long running UI-blocking operation. For example,
we can wrap a network call returning a
User
object by returning the value of the network call inside
the function provided to
.fromCallable()
.


Chapter 2: RxJava Core Components
25
1
Observable
<
User
>
observable
=
Observable
.
fromCallable
(() -> {
2
return
apiService
.
getUserWithId
(123);
3
});
4
observable
.
subscribe
(
user
-> {
5
// Got user with ID 123
6
});
Observable.defer(Callable
extends T>> supplier)
.defer()
is another
Observable
creation method available for deferring a potentially long running
UI-blocking operation. The main distinction between
.defer()
and
.fromCallable()
is that the
former returns an
Observable
in the supplied function.
.defer()
creates an
Observable
that calls an
ObservableSource
factory to create an
Observable
for each new
Observer
that subscribes.
The above code for retrieving a user by ID can then be rewritten as:
1
Observable
<
User
>
observable
=
Observable
.
defer
(() -> {
2
return
Observable
.
just
(
apiService
.
getUserWithId
(123));
3
});
4
observable
.
subscribe
(
user
-> {
5
// Got user with ID 123
6
});
The main reason to use
.defer()
over
.fromCallable()
is if the creation of an
Observable
in itself
is a blocking operation. However, these should in general be rare instances and
.fromCallable()
should suffice.
.fromCallable()
and
.defer()
should be used if the evaluation of a value takes some time to
compute and we would like to delay that computation up until the time of subscription. In contrast,
if
.just()
is used we would not be delaying that computation up until the time of subscription,
instead, the value would be computed immediately.


Chapter 2: RxJava Core Components
26
1
Observable
<
User
>
lazyObservable
=
Observable
.
fromCallable
(() -> {
2
return
apiService
.
getUserWithId
();
3
});
4
5
// At this point, network call has not yet been made.
6
lazyObservable
.
subscribe
(
user
-> {
7
// After subscribing, the network call is made and the user object is return\
8
ed
9
});
10
11
Observable
<
User
>
notLazyObservable
=
Observable
.
just
(
apiService
.
getUserWithId
());
12
13
// At this point, the network call has already been made
14
// which blocks the calling thread.
15
notLazyObservable
.
subscribe
(
user
-> {
16
// After subscribing, the network call has already been
17
// made on construction. The user object is still returned.
18
});
Although on subscription both
Observer
s of

Download 1,47 Mb.

Do'stlaringiz bilan baham:
1   ...   20   21   22   23   24   25   26   27   ...   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