这是我在运行使用 Room
和 RX 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
设置 observer
和 subscribe
一个 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) {
}
}
}
我在 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/