简体   繁体   English

RxJava 3 对 Room 的支持

[英]RxJava 3 support for Room

I am using RxJava3 with Room in my project but I am getting the following error我在我的项目中将 RxJava3 与 Room 一起使用,但出现以下错误

error: Not sure how to convert a Cursor to this method's return type (io.reactivex.rxjava3.core.Flowable>)错误:不确定如何将 Cursor 转换为此方法的返回类型 (io.reactivex.rxjava3.core.Flowable>)

Below is DAO interface method on which I am getting the error下面是我收到错误的 DAO 接口方法

@Query("SELECT * FROM wishlist_table")
Flowable<List<WishListMovie>> getWishList();

I think maybe its because I am using the dependency below in my grade file:我想可能是因为我在成绩文件中使用了以下依赖项:

implementation "androidx.room:room-rxjava2:$room_version"

I tried to find the above dependency for RxJava 3 but I was unable to find it.我试图找到 RxJava 3 的上述依赖项,但我找不到它。

I want to know how can I use RxJava 3 with Room or should I use RxJava 2 instead in my project.我想知道如何将 RxJava 3 与 Room 一起使用,或者我应该在我的项目中使用 RxJava 2。

July 22, 2020 2020 年 7 月 22 日

In Room 2.3.0-alpha02 was added support for RxJava3 types.Room 2.3.0-alpha02中添加了对 RxJava3 类型的支持。 Though it's still in alpha, but you can consider this option.虽然它仍处于 alpha 阶段,但您可以考虑此选项。

According to release description:根据发布说明:

Similar to RxJava2 you can declare DAO methods whose return type are Flowable, Single, Maybe and Completable.与 RxJava2 类似,您可以声明返回类型为 Flowable、Single、Maybe 和 Completable 的 DAO 方法。
Additionally a new artifact androidx.room:room-rxjava3 is available to support RxJava3此外,还有一个新的工件androidx.room:room-rxjava3可用于支持 RxJava3

Problem问题

I want to know how can I use RxJava 3 with Room or should I use RxJava 2 instead in my project.我想知道如何将 RxJava 3 与 Room 一起使用,或者我应该在我的项目中使用 RxJava 2。

Result结果

You can not use RxJava3 with Room "room-rxjava2" dependency.您不能将 RxJava3 与 Room "room-rxjava2" 依赖项一起使用。

Explanation解释

RxJava2 and RxJava3 are different. RxJava2 和 RxJava3 是不同的。 In order to avoid runtime errors (eg during linking) RxJava3 chose different packages.为了避免运行时错误(例如在链接期间),RxJava3 选择了不同的包。 This is why the returned type does not match (different package)这就是返回的类型不匹配的原因(不同的包)

Solution解决方案

Until there is a room-rxjava3 package you have to use RxJava2 as a dependency.在没有 room-rxjava3 package 之前,您必须使用 RxJava2 作为依赖项。

Workaround解决方法

You could checkout room-rxjava2 and change all packages for rxjava3 and compile aginst rxjava3 and then use this package.您可以签出 room-rxjava2 并更改 rxjava3 的所有包并编译 aginst rxjava3,然后使用此 package。

Currently updating a project and having the same problem.目前正在更新项目并遇到同样的问题。

I'm using room-rxjava2 to generate the DAOs (as before) with RxJava2 types.我正在使用 room-rxjava2 生成具有 RxJava2 类型的 DAO(如前所述)。 But for my business logic I'm using https://github.com/akarnokd/RxJavaBridge to convert to RxJava3 types.但对于我的业务逻辑,我使用https://github.com/akarnokd/RxJavaBridge转换为 RxJava3 类型。

Another solution would be not using a reactive type on the Dao and have a reactive wrapper it in a repository另一种解决方案是不在 Dao 上使用响应式类型,而是在存储库中使用响应式包装器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM