\ \ \ \ \ \ \ \ []()
โ๏ธISSUE
\ \ \ \ \ \ \ \ []()
์ฐ์ํ ํ์ ๋ค ๊ธฐ์ ๋ธ๋ก๊ทธ(repository)
โ๏ธISSUE
\ \ \ \ \ \ \ \ []()
Model: ๋ฐ์ดํฐ, ์ํ, ๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋น
View: xml, ui(Fragment, Activity), Presenter์ ์ด๋ฒคํธ ์ ๋ฌ, Presenter๋ก ๋ฐ์ ๋ฐ์ดํฐ ๋ณด์ฌ์ค
Presenter: View๋ก ๋ฐ์ ์ด๋ฒคํธ๋ฅผ ์ฒ๋ฆฌ(๋ก์ง๊ตฌํ)ํ๊ณ Model์ ์ ๋ฐ์ดํธ
Contract์์๋ View ์ธํฐํ์ด์ค์, Presenter ์ธํฐํ์ด์ค๋ฅผ ์ ์ contract๋ง ๋ณด๊ณ ๋ ๋๋ต์ ์ธ ์ฝ๋๋ฅผ ์ ์ ์๋ค.
contract ์๋ view ์ธํฐํ์ด์ค์ presenter ์ธํฐํ์ด์ค๊ฐ ์๋ค.
view ์ธํฐํ์ด์ค๋ ui์์ ์์๋ฐ์ ๊ตฌํํ๋ค.
presenter ์ธํฐํ์ด์ค๋ presenter ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด ์ด ๊ฐ์ฒด๊ฐ presenter ์ธํฐํ์ด์ค๋ฅผ ์์๋ฐ์ ๊ตฌํํ๋ค.
๊ตฌํ๋ presenter ๊ฐ์ฒด๋ ui์์ ๋ณ์๋ก ๊ฐ์ฒด ์์ฑํ์ฌ view์ ์ฐ๋๋๋ค.
โ๏ธISSUE
\ \ \ \ \ \ \ \ []()
Databinding : ํ๋ก๊ทธ๋๋งคํฑ ๋ฐฉ์์ด ์๋๋ผ ์ ์ธ์ ํ์์ผ๋ก ๋ ์ด์์์ UI ๊ตฌ์ฑ์์๋ฅผ ์ฑ์ ๋ฐ์ดํฐ ์์ค์ ๊ฒฐํฉํ ์ ์๋ ์ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
@{} ํ์์ ํตํด findViewById๋ฅผ ๊ตฌํํ ํ์๊ฐ ์๋ค.
๋ ์ด์์ ํ์ผ์์ ๊ตฌ์ฑ์์๋ฅผ ๊ฒฐํฉํ๋ฉด ํ๋์์ ๋ง์ UI ํ๋ ์์ํฌ ํธ์ถ์ ์ญ์ ํ ์ ์์ผ๋ฉฐ ์ด๋ก ์ธํด ํ์ผ์ด ๋์ฑ ๊ฐ์ํ๋๊ณ ์ ์ง๊ด๋ฆฌ ๋ํ ์ฌ์์ง๋ค. ์ฑ ์ฑ๋ฅ์ด ํฅ์๋๋ฉฐ ๋ฉ๋ชจ๋ฆฌ ๋์ ๋ฐ null pointer exceptions๋ฅผ ๋ฐฉ์งํ ์ ์๋ค.
์ฌ์ฉ ๋ฐฉ๋ฒ
dataBinding{
enabled = true
}
lateinit var binding = DataBindingUtil.setContentView(this, [layoutId])
โ๏ธISSUE
\ \ \ \ \ \ \ \ []()
MVVM
: MVVM์ Model-View-ViewModel์ ์ฝ์์ด๋ค. Model์ UI์ ํ์๋ ๋ฐ์ดํฐ ์ ๋น์ฆ๋์ค ๋ก์ง์ ๋ด๋นํ๊ณ View๋ UI๋ฅผ ์๋ฏธํ๋ฉฐ ViewModel์ ์ด๋ฒคํธ ์ฒ๋ฆฌ๋, Model๊ณผ์ ์ธํฐ๋์
๋ฑ์ ๋ด๋นํ๋ค.
AAC๊ฐ ํ์ํ ๊ฒ์ ์๋๋ค. AAC๊ฐ ์์ด๋ ์ถฉ๋ถํ MVVM ๊ตฌ์กฐ๋ฅผ ๋ง๋ค ์ ์๋ค.
AAC(Android Architecture component) : LiveData, ViewModel, Room, Paging ๋ฑ
https://duzi077.tistory.com/196 [๊ฐ๋ฐํ๋ ๋๋์ง]
โ๏ธISSUE
\ \ \ \ \ \ \ \ []()
AAC ViewModel
ViewModel ํด๋์ค๋ UI ๊ด๋ จ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ๊ณ ๊ด๋ฆฌํ๊ธฐ ์ํด ์ค๊ณ๋์์ต๋๋ค. ์ฆ, ์คํฌ๋ฆฐ ํ์ ๊ฐ์ ์ํ ๋ณํ์๋ ๋ฐ์ดํฐ๊ฐ ๋ณด์กด๋ ์ ์๋๋ก ํ์ฉํด์ค๋๋ค.
๋ง์ฝ ์กํฐ๋นํฐ๊ฐ ์ฌ ์์ฑ์ด ๋๋ค๋ฉด, ์ฒ์ ๋ง๋ค์ด์ง ViewModel ์ธ์คํด์ค๋ฅผ ๋ฐ๊ฒ๋ฉ๋๋ค. ์ด ์ธ์คํด์ค๋ฅผ ํธ์ถํ ์กํฐ๋นํฐ ๋๋ ํ๋๊ทธ๋จผํธ๊ฐ destory ๋์ด ๋ฉ๋ชจ๋ฆฌ ํด์ ๊ฐ ๋๊ธฐ์ ๊น์ง ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๊ณ ์์ด์ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๊ณ ์๋ค๊ฐ ํ๋ฉด ํ์ ๊ฐ์ ์ํ ๋ณํ๊ฐ ๋ฐ์ํด๋ ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๊ฒ ๋ฉ๋๋ค. </img src='https://developer.android.com/images/topic/libraries/architecture/viewmodel-lifecycle.png' width='60%'>
sharedViewModel
sharedViewModel?.inputNumber?.postValue(input)
์ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ฃ๋๋ค.sharedViewModel.inputNumber.observe(this,Observer {
it?.let {
// do some thing with the number
}
})
AAC LiveData
LiveData๋ ๊ด์ฐฐ ๊ฐ๋ฅํ ๋ฐ์ดํฐ ํ๋ ํด๋์ค์ด๋ค. ์ผ๋ฐ์ ์ธ ๊ด์ฐฐ๊ฐ๋ฅํจ๊ณผ๋ ๋ค๋ฅด๊ฒ, LiveData๋ Activity, Fragment, Service์ ๊ฐ์ ์ฑ ์ปดํฌ๋ํธ์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ์ธ์ํ๊ณ , ๊ทธ๋๋ก ๋ฐ๋ฅด๋๋ก ๊ตฌํ๋์ด ์๋ค. ์ด๋ LiveData๊ฐ ํ์ฌ ํ์ฑํ๋ ์๋ช ์ฃผ๊ธฐ์ ์๋ ์ฑ ์ปดํฌ๋ํธ์ ์ต์ ๋ฒ(Observer)๋ง ์ ๋ฐ์ดํธ ํ๋๋ก ๋ณด์ฅํ๋ค.
LifecycleOwner ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๊ฐ์ฒด์ ์์ ์ด๋ฃจ๋ ์ต์ ๋ฒ๋ฅผ ๋ฑ๋ก ํ ์ ์๋ค. ์ด๋ฌํ ํน์ฑ์ ํนํ Activity, Fragment์ ์๋ช ์ฃผ๊ธฐ๊ฐ destroy ์ํ๊ฐ ๋์์ ๋, ์ฆ์ ๊ด์ฐฐ์ ์ทจ์ํ๋ฏ๋ก LiveData๊ฐ์ฒด๋ฅผ ์์ ํ๊ฒ ๊ด์ฐฐํ ์ ์๊ณ , ๋ฉ๋ชจ๋ฆฌ ๋ฆญ์ ๋ํ ์ฐ๋ ค๋ ์์ด์ง๋ฏ๋ก ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ ์ ์๋ค.
LiveData ์ฅ์
1. UI๋ฅผ ๋ฐ์ดํฐ ์ํ์ ์ผ์น ์ํฌ ์ ์๋ค.
2. ๋ฉ๋ชจ๋ฆฌ ๋ฆญ์ ์์๋ฅผ ์์จ ์ ์๋ค.
์ต์ ๋ฒ๋ค์ Lifecycle ๊ฐ์ฒด์ ๋ฐ์ธ๋๋๊ณ , ๊ทธ Lifecycle ๊ฐ์ฒด๊ฐ destroy ์ํ๊ฐ ๋๋ฉด ์๋์ ์ผ๋ก ์ง์์ง๋ค. 3. ์ ์ง๋ ์ํฐ๋นํฐ๋ค ๋๋ฌธ์ Crash๋ ์ผ์ด ์๋ค.
์ํฐ๋นํฐ๊ฐ ๋ฐฑ์คํ์ ์์๋์ ๊ฐ์ด, ์ต์ ๋ฒ์ ์๋ช ์ฃผ๊ธฐ๊ฐ ๋นํ์ฑํ ์ํ์ผ ๋๋, ์ด๋ ํ LiveData ์ด๋ฒคํธ๋ ๋ฐ์ง ์๋๋ค.
4. ์ง์ ์๋ช ์ฃผ๊ธฐ๋ฅผ ํธ๋ค๋งํ ํ์๊ฐ ์๋ค.
5. ํญ์ ์ต์ ์ ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๋ค.
6. ๊ตฌ์ฑ(Configuration)์ด ๋ณ๊ฒฝ๋์์ ๋ ์ ์ ํ๊ฒ ๋์ํ๋ค.
Activity๋ Fragment๊ฐ ๊ตฌ์ฑ๋ณ๊ฒฝ๋์ด ์ฌ์์ฑ๋ ๋, ๊ทธ ์ปดํฌ๋ํธ๋ค์ LiveData๋ก ๋ถํฐ ์ฆ์ ์ฌ์ฉ๊ฐ๋ฅํ ์ต์ ๋ฐ์ดํฐ๋ฅผ ์ ๊ณต๋ฐ๋๋ค.
์ฑ๊ธํด ํจํด์ ์ด์ฉํด ์์คํ ์๋น์ค๋ฅผ ๋ํํ์ฌ LiveData๊ฐ์ฒด๊ฐ ๊ณต์ ๋ ์ ์๋๋ก ํ์ฅํ ์ ์๋ค. LiveData์ค๋ธ์ ํธ๊ฐ ์์คํ ์๋น์ค์ ํ๋ฒ ์ฐ๊ฒฐ๋๋ฉด, ๋ฆฌ์์ค๊ฐ ํ์ํ ๋ชจ๋ ์ต์ ๋ฒ๋ LiveData ๊ฐ์ฒด๋ฅผ ๋ณผ ์ ์๋ค.
LiveData ๊ฐ์ฒด์์ฑ ๋ฐฉ๋ฒ
Architecture ViewModel
๊ณผ AAC ViewModel
๊ฐ์ ์ฐจ์ด์
onSaveInstanceState()
๋ฉ์๋๋ฅผ ์จ์ onCreate()
์์ ๋ค์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ ์ ์์ง๋ง ์ด๋ฅผ ๊ตฌํํ์ง ์์๋ ๋๋ค.https://duzi077.tistory.com/196 [๊ฐ๋ฐํ๋ ๋๋์ง]
https://developer.android.com/topic/libraries/architecture/viewmodel
โ๏ธISSUE
- ์์ ๊ฐ์ด binding๋์ง ์๋๋ค ํด๊ฒฐ
\ \ \ \ \ \ \ \ []()
Koin
DI(Dependency injection): ๊ตฌ์ฑ์์๊ฐ์ ์์กด ๊ด๊ณ๊ฐ ์์ค์ฝ๋ ๋ด๋ถ๊ฐ ์๋ ์ธ๋ถ ์ค์ ํ์ผ๋ฑ์ ํตํด ์ ์๋๊ฒํ๋ ๋์์ธ ํจํด์ค ํ๋์ ๋๋ค
๋ถ๋ฆฌ์์ผ ๋์ผ๋ฉด ๊ฐ์ฒด์ ์์ฑ๊ณผ ์ฌ์ฉ์ ๋ถ๋ฆฌ์ํฌ ์ ์๊ณ , ์ฌ์ฌ์ฉ์ด ์ ์ฐํด์ง๋๋ค.
ํค์๋
โ๏ธISSUE
- ๋ชจ๋ํ๋ ์ด๋ค ๊ธฐ์ค์ผ๋ก ๋๋๋๊ฐ? [ํด๊ฒฐ]()
- Service, Datasource, Repository๋ฅผ ๋ชจ๋ํํ๋ ค๋ฉด ๊ฐ๊ฐ์ ๊ฐ์ฒด๋ค์ ์ธ์, ์์, ๋ณ์ ์ค ์ด๋ค ๊ฒ์ ๊ตฌํํด์ผ ํ๋๊ฐ? [ํด๊ฒฐ]()