I am developing in Adnroid
RxJava
with Room
.
I use retrofit to call API and update the value in database like the following code:
override fun certifyMail(userInfo: Userinfo): Observable<Int> {
return securityApi.certifyUserInfo(userInfo)
.map {
//return Observable<Status> here
when(it.status){
"OK" -> {
it
}
else -> {
throw Exception(it.msg!!)
}
}
}
.flatMap {
userInfoDataSource.updateUserCertifyMailInfo(userInfo.account,1)
//How to convert the return type to Observable<Int> from Int.
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
When the response is OK
from API, I will update the value in database like the following.
@Query("UPDATE certify_info SET value = :value WHERE account = :account")
fun updateCertifyMailConfirm(account:String,value: Int):Int
But the return type of database is Int
. How to covert it to Observable<Int>
?
Thanks in advance.
Room library can return RxJava's types.
Add implementation "androidx.room:room-rxjava2:2.2.0-rc01"
library to your project
Checkout this great article for better understanding.
Also checkout Google official sample about this
And simply edit your updateUserCertifyMailInfo
method in one of this ways (choose one that suits more):
With Maybe:
@Query("UPDATE certify_info SET value = :value WHERE account = :account")
fun updateCertifyMailConfirm(account:String,value: Int):Maybe<Int>
With Single:
@Query("UPDATE certify_info SET value = :value WHERE account = :account")
fun updateCertifyMailConfirm(account:String,value: Int):Single<Int>
With Flowable/Observable:
@Query("UPDATE certify_info SET value = :value WHERE account = :account")
fun updateCertifyMailConfirm(account:String,value: Int):Flowable<Int>
Important! Read carefully about how every type behaves with Room Queries
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.