Reactive Programming on Android with RxJava


Marble diagram of .flatMap()



Download 1,47 Mb.
Pdf ko'rish
bet27/60
Sana20.04.2022
Hajmi1,47 Mb.
#566724
1   ...   23   24   25   26   27   28   29   30   ...   60
Bog'liq
reactiveandroid

Marble diagram of .flatMap()
A common real-world example of using
.flatMap()
is for implementing nested network calls. Say
we have an
Observable
, and for each
User
, we’d like to make a network call to retrieve that
user’s detailed information.
Assume we have the following methods available:
1
Observable
<
User
>
getUser
(
int
userId
) {
2
// Return an Observable that performs a network
3
// request to retrieve a User with ID `userId`
4
}
5
6
Observable
<
UserDetail
>
getUserDetail
(
User user
) {
7
// Return an Observable that performs a
8
// network request to retrieve details about `user`
9
}
Chaining
.getUser()
with
.flatMap()
and having the function provided in
.flatMap()
return a
.getUserDetail()
gives us:


Chapter 3: Operators
31
1
Observable
<
User
>
userObs
=
getUser
(123);
2
Observable
<
UserDetail
>
detailObs
=
userObs
.
flatMap
(
user
-> {
3
return
getUserDetail
(
user
);
4
});
5
detailObs
.
subscribe
(
userDetail
-> {
6
// Received user's detail information here
7
});
Now you might still be wondering, what is the different between
.map()
and
.flatMap()
and when
should you be using one over the other? Perhaps a good way to see the difference between the two
is by changing our above example to use
.map()
instead.
1
Observable
<
User
>
userObs
=
getUser
(123);
2
Observable
<
Observable
<
UserDetail
>>
detailObs
=
userObs
.
map
(
user
-> {
3
return
getUserDetail
(
user
);
4
});
5
detailObs
.
subscribe
(
userDetailObservable
-> {
6
// Received Observable here, need to subscribe
7
// to it to receive its emission
8
userDetailObservable
.
subscribe
(
userDetail
-> {
9
// Got user detail
10
});
11
});
Using
.map()
, the observer receives an object of type
Observable
and thus needs to
subscribe to it to receive its emissions. With
.flatMap()
, this is not the case; the observer receives
a
UserDetail
, which, in our example, is the result the observer is interested in.
It is quite cumbersome for an observer to receive
Observable
s. Imagine for a second that our
.getUser()
method is implemented such that the observer receives updates to a
User
object (i.e.
the
Observable
stream can return multiple objects over time). Using
.map()
, we would receive
multiple
Observable
objects which we would have to individually subscribe to and manage.
This is exactly the problem that
.flatMap()
solves. The operator will handle managing
Observable
sequences emitted by the function you provide it so that the observer to
.flatMap()
receives a

Download 1,47 Mb.

Do'stlaringiz bilan baham:
1   ...   23   24   25   26   27   28   29   30   ...   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