【发布时间】:2019-02-03 11:19:42
【问题描述】:
我正在研究在我最新的 Android 应用程序中使用 RxJava。
我有两个相关更新的数据库模型对象列表
ListDB1 和 ListDB2
我试图实现的逻辑如下
1). For each item in ListDB1
1.1). Transform it to a Network model object
1.2). Execute an Update RESTful API
2). Once all network updates have completed successfully
2.1). Persist ListDB1 to my local database.
2.2). Persist ListDB2 to my local database.
到目前为止,我有这段代码应该调用我的网络 API
Observable.just(getDatabaseList())
.subscribeOn(Schedulers.io())
.flatMapIterable(x -> x)
.flatMap(database -> transformDatabase(database, DB_1_MAPPER))
.doOnNext(NetworkController::updateRemote)
.observeOn(AndroidSchedulers.mainThread())
.doOnComplete(getOnComplete())
.doOnError(getOnError())
.subscribe();
虽然没有执行任何 API 调用
我宁愿使用Single Observable 作为我的 API 调用以 Single<Response> 响应,但是我看不到如何使用 Single 实现 Observable.just()。
我也看不到如何通过分别处理网络调用的每个列表项来开始我的 Rx 过程,然后使用完整列表执行数据库调用,因为我使用 Realm 作为我的本地数据库,它可以接受数据库对象列表单次更新。
在伪代码中,我的过程类似于:
for each database list item
convert to network model item
call remote update API
When all all network calls are successful
update database with ListDB1
update database with ListDB2
end
这可能在一个 Rx 进程“流”中实现吗?
【问题讨论】: