[Android] MediatorLiveData์™€ Transformations

Hanbinยท2021๋…„ 9์›” 8์ผ
3

Android

๋ชฉ๋ก ๋ณด๊ธฐ
4/6
post-thumbnail

๐Ÿ’ก MediatorLiveData

๋‹ค๋ฅธ LiveData ๊ฐ์ฒด๋ฅผ observeํ•˜๊ณ  ํ•ด๋‹น ๊ฐ์ฒด์˜ OnChanged ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›๋Š” LiveData ํ•˜์œ„ ํด๋ž˜์Šค์ด๋‹ค.

addSource() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด observeํ•  LiveData์™€ ์ˆ˜ํ–‰ํ•  ๋กœ์ง์„ ์ „๋‹ฌํ•˜์—ฌ ์ถ”๊ฐ€ํ•˜๊ณ , removeSource() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ œ๊ฑฐํ•œ๋‹ค.

MediatorLiveData์˜ ํ™œ์„ฑ/๋น„ํ™œ์„ฑ ์ƒํƒœ๋ฅผ onActive(), onInactive() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ถ”๊ฐ€ํ•œ ํ•˜์œ„ LiveData ๊ฐ์ฒด์— ์ „๋‹ฌํ•œ๋‹ค.

์ •๋ฆฌํ•ด๋ณด๋ฉด MediatorLiveData์— ํ•˜๋‚˜ ๋˜๋Š” ์—ฌ๋Ÿฌ ๊ฐœ์˜ LiveData๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ถ”๊ฐ€๋œ LiveData์˜ onChanged ์ฝœ๋ฐฑ์„ ๋ฐ›์•„ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ์‚ดํŽด๋ณด์ž.

MediatorLiveData.java

MediatorLiveData๋Š” MutableLiveData๋ฅผ ์ƒ์†ํ•˜๊ณ  ์žˆ์–ด ๊ฐ’ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋˜ํ•œ LiveData์™€ Source๋ฅผ ์ €์žฅํ•  Map์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

Source

Source์—๋Š” LiveData์™€ onChanged ์ถ”์ƒ ๋ฉ”์„œ๋“œ๊ฐ€ ํฌํ•จ๋œ Observer ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ Observer๋ฅผ implements ํ•˜๊ณ  ์žˆ๋‹ค.

์ „๋‹ฌ๋ฐ›์€ LiveData์˜ onChanged() ์ฝœ๋ฐฑ ๋“ฑ๋ก์„ ์œ„ํ•œ plug(), unplug() ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  onChanged() ์ฝœ๋ฐฑ์„ ๋ฐ›์œผ๋ฉด mObserver.onChanged(v);๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

์—ฌ๊ธฐ์„œ Source๋Š” LiveData๋ฅผ ์˜ต์ ธ๋น™ํ•˜๋ฉฐ ๋ณ€ํ™”๋ฅผ ๊ฐ์ง€ํ–ˆ์„ ๋•Œ ์ „๋‹ฌ๋ฐ›์€ Observer์˜ onChanged๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

addSource()

Source ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌ๋ฐ›์€ LiveData์™€ Observer๋ฅผ ์ด์šฉํ•ด ์ƒ์„ฑํ•ด ์ฃผ๊ณ  mSources์— ์ €์žฅํ•œ ๋’ค MediatorLiveData๊ฐ€ ์•กํ‹ฐ๋ธŒ ์ƒํƒœ์ผ ๊ฒฝ์šฐ ์ƒ์„ฑํ•œ Source๋ฅผ plug() ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•ด ์ค€๋‹ค.

removeSource()

์ „๋‹ฌ๋ฐ›์€ LiveData๋ฅผ mSources์—์„œ ์ œ๊ฑฐํ•˜๊ณ  ํ•ด๋‹น Source๊ฐ€ null์ด ์•„๋‹ ๊ฒฝ์šฐ unplug() ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

onActive(), onInactive()

Activity ๋ฐ Fragment์˜ Lifecycle ์ƒํƒœ์— ๋”ฐ๋ผ onActive(), onInactive()๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ, mSources์— ์žˆ๋Š” ๋ชจ๋“  Source๋ฅผ plug(), unplug() ์ƒํƒœ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

MediatorLiveData Example

EditText์˜ ์ž…๋ ฅ๊ฐ’์„ ๋ฐ›๋Š” Two-way-binding์„ ์‚ฌ์šฉํ•œ MutableLiveData ๋‘ ๊ฐ€์ง€๊ฐ€ ์žˆ๊ณ  MediatorLiveData ํ•˜๋‚˜์™€ ๊ทธ Backing field์ธ LiveData๊ฐ€ ํ•˜๋‚˜ ์žˆ๋‹ค.

init ๋ธ”๋ก์„ ์‚ดํŽด๋ณด๋ฉด addSource ๋ฉ”์„œ๋“œ๋ฅผ ์ด์šฉํ•ด ๋‘ ๊ฐ€์ง€ MutableLiveData์™€ ์ฝœ๋ฐฑ ์‹œ ์ˆ˜ํ–‰ํ•  ์ฝ”๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

userId, userPassword๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ checkProcess() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ _isProcessComplete์˜ ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒƒ์ด๋‹ค.

๐Ÿ’ก Transformations

LiveData๋ฅผ ๋ณ€ํ™˜ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค.

Transformations์— ํฌํ•จ๋œ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ observer์˜ lifecycle์— ๊ฑธ์ณ ์ •๋ณด๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.

Observer๊ฐ€ ๋ฐ˜ํ™˜๋œ LiveData ๊ฐ์ฒด๋ฅผ ์˜ต์ €๋น™ํ•˜์ง€ ์•Š๋Š” ํ•œ ๊ณ„์‚ฐ๋˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ ์ด์œ ๋Š” Transformations์ด lazy ํ•˜๊ฒŒ ๊ณ„์‚ฐ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋ช…์‹œ์  ํ˜ธ์ถœ์ด๋‚˜ ์ข…์†์„ฑ์„ ์š”๊ตฌํ•˜์ง€ ์•Š๊ณ  lifecycle ๊ด€๋ จ ๋™์ž‘์ด ์•”์‹œ์ ์œผ๋กœ ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค.

Transformations.map() - ์ผ๋Œ€์ผ ์ •์  ๋ณ€ํ™˜

์•ž์„œ ์•Œ์•„๋ณธ MediatorLiveData๋ฅผ ํ™œ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

์ƒˆ๋กœ์šด MediatorLiveData ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด ์ „๋‹ฌ๋ฐ›์€ LiveData์™€ mapFunction์„ ๋”ํ•ด์ฃผ๋ฉฐ ๋ฐ˜ํ™˜ํ•œ๋‹ค. onChanged()์—์„œ mapFunction์˜ ๊ฒฐ๊ณผ๋ฅผ MediatorLiveData ๊ฐ์ฒด์— set ํ•œ๋‹ค. ์ „๋‹ฌ๋ฐ›์€ LiveData์˜ ๊ฐ’์ด ๋ณ€๊ฒฝ๋˜๋ฉด ๋ฐ˜ํ™˜๋œ MediatorLiveData ๊ฐ์ฒด์— set ์ž‘์—…์ด ์ˆ˜ํ–‰๋  ๊ฒƒ์ด๋‹ค.

mapFunction์€ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋œ๋‹ค.

Transformations.map() Example

userLiveData๋ฅผ ์˜ต์ ธ๋น™ ํ•˜๊ณ  ์žˆ์œผ๋ฉฐ ๊ฐ’ ๋ณ€๊ฒฝ์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋ฅผ ์„ค์ •ํ•ด ์ค€๋‹ค.

Transformations.switchMap() - ์ผ๋Œ€์ผ ๋™์  ๋ณ€ํ™˜

onChanged()๋ฅผ ์‚ดํŽด๋ณด๋ฉด ํ•จ์ˆ˜์˜ ๊ฒฐ๊ณผ๋กœ ๋ฐ›์€ LiveData์™€ mSource๋ฅผ ๋น„๊ตํ•˜์—ฌ ๋™์ผํ•˜๋ฉด ์•„๋ฌด ์ž‘์—…๋„ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๋‹ค๋ฅผ ๊ฒฝ์šฐ ๊ธฐ์กด Source๋Š” ์ œ๊ฑฐํ•˜๋ฉฐ ๊ฒฐ๊ณผ๋กœ ๋ฐ›์€ LiveData๋ฅผ map()๊ณผ ๋™์ผํ•œ ๋ฐฉ์‹์œผ๋กœ ์ถ”๊ฐ€ํ•ด ์ค€๋‹ค.

switchMapFunction์€ ๋ฉ”์ธ ์Šค๋ ˆ๋“œ์—์„œ ์‹คํ–‰๋œ๋‹ค.

Transformations.switchMap() Example

setUserId() ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ repository.getUserById(it) ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉฐ ๊ธฐ์กด LiveData์™€ ๋™์ผํ•œ์ง€ ๋น„๊ตํ•œ๋‹ค. ๋‹ค๋ฅผ ๊ฒฝ์šฐ ํ•ด๋‹น ๊ฒฐ๊ณผ๊ฐ€ ์ ์šฉ๋œ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€