2

我正面临这个问题。我使用房间来创建我的应用程序的本地数据库。假设我有一个实体调用用户和一个 UserDao。它看起来像这样:

@Dao
interface UserDao: BaseDao<User>{
    @Query("SELECT * FROM user WHERE remote_id = :remoteId")
    fun getUser(remoteId : Long) : Single<User>
}

在这一点上没有什么奇怪的,但我想做的是能够改变这些函数中的返回类型。我想要这个,因为有时我需要一个 Single,但在其他情况下我需要一个 Flowable。在这两种情况下,查询是相同的,唯一比 chagues 是返回的类型,我不想做这样的事情。

@Dao
interface UserDao: BaseDao<User>{

    @Query("SELECT * FROM user WHERE remote_id = :remoteId")
    fun getUserSingle(remoteId : Long) : Single<User>

    @Query("SELECT * FROM user WHERE remote_id = :remoteId")
    fun getUserFlowable(remoteId : Long) : Flowable<User>
 } 

知道如何以干净的方式做到这一点吗?

4

2 回答 2

1

You can just use your Single<User> and call toFlowable() on it. Something like getUserSingle().toFlowable() on your dao object.

EDIT

You can us flowable operator and call it something like getUserFlowable.firstOrError() . because from the documentation

Returns a Single that emits only the very first item emitted by this Flowable or signals a {@link NoSuchElementException} if this Flowable is empty

于 2018-03-02T20:56:47.820 回答
-1

在 dao 界面中进行一些更改,如下所示

    @Dao
interface AchivementDao {
    @Query("SELECT * FROM Achivement") // pass your table name
    fun getUserAchivement()=mutableListOf<Achivement>() // pass your pojo class name

    @Query("SELECT * FROM Achivement WHERE id=:arg0")
    fun getUserAchivement(id:Int):Achivement // pass your return object of class

}
于 2018-03-03T06:27:40.417 回答