π λ°μ΄ν°λ² μ΄μ€ μ‘μΈμ€μ κ°μ΄ λ¬΄κ±°μ΄ μ€νμ MainThread κ° μλ λ€λ₯Έ Threadμμ μ€νν΄μΌ νλλ°, μ΄λ² μμ μμλ Rxjava3 λ₯Ό μ μ©μμΌμ Room DB λ₯Ό ꡬννμλ€.
plugins {
...
id 'kotlin-kapt'
}
dependencies {
...
implementation "androidx.room:room-runtime:2.5.1"
annotationProcessor "androidx.room:room-compiler:2.5.1"
implementation "androidx.room:room-rxjava3:2.5.1"
implementation 'io.reactivex.rxjava3:rxandroid:3.0.2'
implementation 'io.reactivex.rxjava3:rxjava:3.1.5'
}
@Dao
public interface TestDao {
@Query("SELECT * FROM testInfo")
Single<List<TestInfo>> getAll();
@Insert
Completable addTest(TestInfo testInfo);
@Update
Completable updateTest(TestInfo testInfo);
}
// μ¬μ©
TestDataBase.getInstance(context)
.testDao()
.addTest(testInfo)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe();
TestDataBase.getInstance(context)
.testDao()
.getAll()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.doOnSuccess(list -> {
// μ±κ³΅
}).subscribe();