Room 使用详解
添加依赖
AndroidX版本依赖
dependencies {
def room_version = "2.2.0-alpha01" // 2.1.0 for latest stable version
implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - Kotlin Extensions and Coroutines support for Room
implementation "androidx.room:room-ktx:$room_version"
// optional - RxJava support for Room
implementation "androidx.room:room-rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "androidx.room:room-guava:$room_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
}
对于Android X之前的Room版本请使用下面的依赖
dependencies {
def room_version = "1.1.1"
implementation "android.arch.persistence.room:runtime:$room_version"
annotationProcessor "android.arch.persistence.room:compiler:$room_version" // For Kotlin use kapt instead of annotationProcessor
// optional - RxJava support for Room
implementation "android.arch.persistence.room:rxjava2:$room_version"
// optional - Guava support for Room, including Optional and ListenableFuture
implementation "android.arch.persistence.room:guava:$room_version"
// Test helpers
testImplementation "android.arch.persistence.room:testing:$room_version"
}
Android推荐的架构组件
这张图给的不错
查询返回类型
查询类型 | 协程 | Rxjava | 生命周期 |
---|---|---|---|
可观察读取 | Flow | Flowable、Publisher、Observable | LiveData |
单次读取 | suspend fun | Single、Maybe | 无 |
单次写入 | suspend fun | Single、Maybe、Completable | 无 |
问题
参考链接
Android Room 使用详解
Android Room 使用详解
Android Room使用
Room and RxJava
Save data in a local database using Room
Room Persistence Library(官网文档翻译)
androd Room和LiveData
Android Room 使用以及配合 RxJava
Android Room 使用指南
Android Room官方文档