英文:
Return value from DB operation in Single<Long>
问题
我在我的数据库(Room)中添加一条记录,并返回插入记录的ID。例如,我添加一行,它返回数字5(类型为Long)。
当该操作完成时,我需要将该ID传递给另一个函数。当第二个函数完成时,我需要返回从步骤1获取的ID。
我正在使用RxJava,并订阅响应,因为我需要在完成时执行另一个操作,使用DB记录ID。
以下是代码:
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveUserData(date: UserDataDatabaseModel): Long
这返回行ID的Long。
我这样调用它:
override fun submitUserData(): Single<Long> {
    var rowId: Long = -1
    return Completable.fromAction {
        rowId = dao.saveUserData(getUserData())
    }.andThen { 
        uploadUserData(rowId)
    }.andThen(Single.just(rowId))
}
在调用点,我这样做:
submitUserData()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnError { throwable ->
            throwable.printStackTrace()
        }
        .subscribe({ rowId ->
            //处理rowId
        },{
            //处理错误
        })
问题是代码从不返回到subscribe。如果我调试并使用LogCat等,那么我看到代码被调用的最后一点是在.andThen(uploadUserData())代码中。
所以我认为我需要替换代码中的Completable部分,并用类似Single.create()的东西替换它。我已经尝试过这个并且遇到了类似的问题,代码停止在第一个块之后执行,就好像它没有被订阅一样。
有人能帮助我解决这个问题吗?
谢谢
英文:
I am adding a record in my database (Room) and it returns the ID of the inserted record. For example, I add a row and it returns the number 5 (which is a Long).
When that operation is complete, I need to pass that ID to another function. When this 2nd function is complete, I need to return the ID which I got from step 1.
I am using RxJava and I am subscribing to the response, as I need to perform another action when this is complete, using the DB record ID.
See below:
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun saveUserData(date: UserDataDatabaseModel): Long
This returns the Long of the row ID.
I call it like this:
override fun submitUserData(): Single<Long> {
    var rowId: Long = -1
    return Completable.fromAction {
        rowId = dao.saveUserData(getUserData())
    }.andThen { 
        uploadUserData(rowId)
    }.andThen(Single.just(rowId))
}
At the call point, I do this:
submitUserData()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .doOnError { throwable -> throwable.printStackTrace() }
        .subscribe({ rowId ->
            //handle rowId
        },{
            //handle error
        })
The issue I have is that the code never returns to the subscribe.
If I debug and use LogCat etc, then the last place I see the code being called is in the .andThen(uploadUserData()) code.
So I think I need to replace the Completable part of the code and replace it with something like Single.create(). I did try this already and got a similar problem where the code stopped executing after the first block, like it wasn't being subscribed to.
Can anyone help me with this?
Thanks
答案1
得分: 2
首先,您不需要在响应式流之外使用额外的变量,现在
- 
为什么不直接调用
Single.fromCallable{
dao.saveUserData(getUserData()).apply {
uploadUserData(it) //非响应式调用
}
} - 
如果您确实需要将调用拆分到不同的Observables
Single.fromCallable{ dao.saveUserData(getUserData()) }
.flatMap { rowId -> uploadUserData(rowId).map {_ -> rowId }} 
英文:
First of all you don't need additional variable outside the reactive flow, and now
- 
why don't you just call
Single.fromCallable{
dao.saveUserData(getUserData()).apply {
uploadUserData(it) //non rx call
}
} - 
if you really need to split calls to different Observables
Single.fromCallable{ dao.saveUserData(getUserData()) }
.flatMap { rowId -> uploadUserData(rowId).map {_ -> rowId }} 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论