android - 如何解决错误 : "Not sure how to handle insert method' s return type"in implement room with RX java

标签 android kotlin rx-java2 android-room

这是我在运行使用 RoomRX java 开发的示例项目后出现的错误:

e: /home/milad/workspace/android/WalletMVVM/app/build/tmp/kapt3/stubs/debug/com/example/walletmvvm/data/dao/CurrencyDao.java:15: error: Not sure how to handle insert method's return type.
    public abstract io.reactivex.Single<com.example.walletmvvm.data.model.CurrencyModel> insert(@org.jetbrains.annotations.NotNull()

我搜索了很多,但我没有找到任何东西。

我可以使用 RX java 实现 Retrofit 并使用它,我可以使用 RX java 在其他线程中运行 Room 查询 但我还不能用 RX java 设置 observersubscribe 一个 observable

一切都很好

这是我的 Dao :

@Dao
interface CurrencyDao {

        @Query("SELECT * FROM ${DbConstants.TABLE_CURRENCY} ORDER BY ${DbConstants.NAME_CURRENCY_MODEL} ASC")
        fun getCurrencyList(): Observable<List<CurrencyModel>>

        @Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insert(currencyModel: CurrencyModel): Single<CurrencyModel>
    }

这是我的存储库:

companion object {

    // For Singleton instantiation
    @Volatile
    private var instance: CurrencyRepository? = null

    fun getInstance(currencyDao: CurrencyDao) =
        instance ?: synchronized(this) {
            instance ?: CurrencyRepository(currencyDao).also { instance = it }
        }
}

fun getCurrencyList(): Observable<List<CurrencyModel>>? {

    return currencyDao.getCurrencyList()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())

}

这是我的 fragment ,我观察者数据:

private fun initObservers() {
    val getCurrencyListObserver = currencyViewModel.getCurrencyLists()
    getCurrencyListObserver?.subscribeWith(getCurrenciesFromDatabase())
}

private fun getCurrenciesFromDatabase() : Observer<List<CurrencyModel>> {


    return object : Observer<List<CurrencyModel>> {
        override fun onComplete() {

        }

        override fun onSubscribe(d: Disposable) {

        }

        override fun onNext(t: List<CurrencyModel>) {

            t.let {

                showResult(it.size.toString() + " items")
                setRecyclerData(it)
                binding.listSize = it.size
            }
        }

        override fun onError(e: Throwable) {

        }
    }
}

为了实现这个我研究了this在中学和学习中this

我在 googlesamples/android-architecture-components 中看到了关于 Kotlin 的示例代码但他们不使用观察者


更新:

因为在那之后我删除了 single 我不能使用:

currencyDao.insert(currency)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe()

并且 currencyDao.insert(currency) 无法访问 .subscribeOn() 和其他


更新:

当我将返回类型更改为 Long 类型时我解决了这个问题,但我遇到了新的错误

 @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(currencyModel: CurrencyModel): Single<Long>

新的错误是:

e: /home/milad/workspace/android/WalletMVVM/app/build/tmp/kapt3/stubs/debug/com/example/walletmvvm/data/dao/CurrencyDao.java:11: error: Not sure how to convert a Cursor to this method's return type (java.lang.Object).
    public abstract io.reactivex.Observable<java.lang.Object> getCurrencyList();

更新:

我将 insert 返回类型更改为 Completable 并修复了它

最佳答案

你的:

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(currencyModel: CurrencyModel): Single<CurrencyModel>

应该返回 Completable 而不是 Single

        @Insert(onConflict = OnConflictStrategy.REPLACE)
        fun insert(currencyModel: CurrencyModel): Completable

那是因为当你的操作完成后不需要返回任何东西。它只是进入后台的onComplete block 。

关于android - 如何解决错误 : "Not sure how to handle insert method' s return type"in implement room with RX java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56971171/

相关文章:

android - 使用 .just() 运算符时如何添加延迟

android - 二单入一链rxjava2

android - PendingIntent 不适用于通知

android - stub 索引指向没有 PSI 的文件 : com. intellij.openapi.fileTypes.UnknownFileType@23713b43

android - android中的语音通话录音应用

kotlin - Kotlin 中的 "if"与 "takeIf"?

kotlin - 在 Kotlin Android 中用于初始化 PointF 或 RectF 时,Float 原语神奇地变为 0

java - 将 Objective-C 结构数组转换为 Java

Android Studio 模拟器首次出现互联网连接问题

android - 如何使用 Retrofit 和 RXJava 调用相同的 API N 次。我现在使用以下方法