Flow의 예제를 살펴보다 보면 종종 ROOM과 같이 사용하는 것을 볼 수 있습니다.
왜 이렇게 같이 사용하고 어떻게 사용할까요?
설명을 한번 보도록 하겠습니다.
Coroutines support in Room has been increasing at every release: Room 2.1 added coroutines support for one-shot read / write operations and with Room 2.2 we now have support for observable reads with Flow enabling you to get notified of changes in your database.
Start getting notified of the changes in your database by using observable reads with Flow! Together with other coroutines support added in Jetpack libraries like lifecycle-aware coroutine scopes, suspend lifecycle-aware coroutines or the transformation from Flow to LiveData, you can now use coroutines and Flow throughout your entire application.
일단 기본적으로 ROOM 데이터베이스를 이용하는 방법을 한번 살펴보겠습니다.
아래와 같이 코드를 작성해줄 수 있습니다.
id 'kotlin-kapt'
// ROOM
def roomVersion = "2.4.0"
implementation("androidx.room:room-runtime:$roomVersion")
annotationProcessor("androidx.room:room-compiler:$roomVersion")
// To use Kotlin annotation processing tool (kapt)
kapt("androidx.room:room-compiler:$roomVersion")
// Coroutine
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0")
// optional - Kotlin Extensions and Coroutines support for Room
implementation("androidx.room:room-ktx:$roomVersion")
// ViewModelScope
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.1")
implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
class MainActivity : AppCompatActivity() {
lateinit var viewModel: MainViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
findViewById<Button>(R.id.insert).setOnClickListener {
viewModel.add("aa")
}
// viewModel.readAllData.observe(this, androidx.lifecycle.Observer {
// Log.d("DATA", it.toString())
// })
lifecycle.coroutineScope.launch {
viewModel.readData().collect() {
Log.d("DATA2", it.toString())
}
}
// lifecycle.coroutineScope.
}
}
class MainViewModel(application: Application) : AndroidViewModel(application) {
// var readAllData : LiveData<List<TextEntity>>
val context = getApplication<Application>().applicationContext
val db = TextDatabase.getDatabase(context)
// init {
// readAllData = db.textDao().getAllData().asLiveData()
// }
fun add(text : String) = viewModelScope.launch(Dispatchers.IO) {
db.textDao().insert(TextEntity(0, text))
}
fun readData() : Flow<List<TextEntity>> = db.textDao().getAllData()
}
기본적인 room 코드입니다.
여기에서 Main에서 flow를 관찰해줄 수 있고
lifecycle.coroutineScope.launch {
viewModel.readData().collect() {
Log.d("DATA2", it.toString())
}
}
ViewModel에서 asLiveData로 관찰해줄 수 있습니다.
// var readAllData : LiveData<List<TextEntity>>
// init {
// readAllData = db.textDao().getAllData().asLiveData()
// }
- 참조
https://developer.android.com/codelabs/basic-android-kotlin-training-intro-room-flow#8
https://medium.com/@lukaszburcon/flow-and-livedata-in-mvvm-architecture-6f8879b96ce0
https://github.com/HanYeop/AndroidStudio-Practice/tree/master/RoomEx
https://tourspace.tistory.com/342
https://medium.com/androiddevelopers/room-flow-273acffe5b57
https://hungseong.tistory.com/33
'Android Jetpack' 카테고리의 다른 글
ROOM + FLOW CRUD - 2 (0) | 2022.07.11 |
---|---|
ROOM + FLOW CRUD - 1 (0) | 2022.07.03 |
Coroutine flow (0) | 2022.06.19 |
WorkManager 고유작업 (0) | 2022.05.15 |
WorkManager 주기적 실행 (0) | 2022.05.02 |