Room使用

本文详细介绍了Android Room数据库的使用方法,包括如何添加依赖、不同版本的依赖配置,以及Room与Kotlin、RxJava、Guava等技术的结合使用。同时,文章还探讨了Room在查询、写入操作中的不同类型支持,如协程、RxJava、LiveData等。

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生命周期
可观察读取FlowFlowable、Publisher、ObservableLiveData
单次读取suspend funSingle、Maybe
单次写入suspend funSingle、Maybe、Completable

问题

  1. Update some specific field of an entity in android Room
  2. Does Room support entity inheritance?

参考链接

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官方文档

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值