RxJava在一个observable中处理多个回调

时间:2017-11-29 05:01:14

标签: java android kotlin observable rx-java2

我有一个Observable,里面有多个回调。第一个是从服务器获取列表,第二个是项目更改回调,它告诉列表中的项是否已更改。这是我目前的实施:

Flowable.create({ emitter ->
        val offlineManager = OfflineManager.getInstance(context)

        offlineManager.listOfflineRegions(object : OfflineManager.ListOfflineRegionsCallback {

            override fun onList(offlineRegions: Array<OfflineRegion>) {
                offlineRegionsFetch(offlineRegions, emitter)
            }

            override fun onError(error: String) {
                emitter.onError(IllegalStateException("Saved offline regions read error"))
            }
        })
    }, BackpressureStrategy.BUFFER)

   private fun offlineRegionsFetch(offlineRegionList: Array<OfflineRegion>, emitter: FlowableEmitter<List<OfflineMap>>) {

    if (offlineRegionList.isNotEmpty()) {
        val count = 0

        val offlineMaps = offlineRegionList.map { OfflineMap(it.id.toString(), OfflineMapManager.getRegionName(it), 0L, OfflineMapStatus(FETCH)) } as ArrayList<OfflineMap>

        offlineRegionList.forEachIndexed { index, offlineRegion ->
            offlineRegion.getStatus(object : com.mapbox.mapboxsdk.offline.OfflineRegion.OfflineRegionStatusCallback {

                override fun onStatus(status: OfflineRegionStatus) {
                    if (status.isComplete) {
                        offlineMaps[index] = OfflineMap(offlineRegion.id.toString(), OfflineMapManager.getRegionName(offlineRegion), status.completedResourceSize, OfflineMapStatus(SUCCESS))
                        count + 1
                    } else if (status.downloadState == OfflineRegion.STATE_INACTIVE) {
                        offlineMaps[index] = OfflineMap(offlineRegion.id.toString(), OfflineMapManager.getRegionName(offlineRegion), status.completedResourceSize, OfflineMapStatus(PAUSED))
                        count + 1
                    } else if (!status.isRequiredResourceCountPrecise) {
                        offlineMaps[index] = OfflineMap(offlineRegion.id.toString(), OfflineMapManager.getRegionName(offlineRegion), 0L, OfflineMapStatus(DOWNLOAD_STARTED))
                        count + 1
                    } else if (!status.isComplete && status.isRequiredResourceCountPrecise) {
                        val percentage = 100.0 * status.completedResourceCount / status.requiredResourceCount
                        offlineMaps[index] = OfflineMap(offlineRegion.id.toString(), OfflineMapManager.getRegionName(offlineRegion), status.completedResourceSize,
                                OfflineMapStatus(PROGRESS, offlineRegion.id.toString(), Math.round(percentage)))
                        count + 1
                    }
                }

                override fun onError(error: String) {
                    offlineMaps[index] = OfflineMap(offlineRegion.id.toString(), OfflineMapManager.getRegionName(offlineRegion), -1L,
                            OfflineMapStatus(ERROR, offlineRegion.id.toString(), errorCode = errorMap(error), errorMsg = error))
                    count + 1
                }
            })
        }

        if (count == offlineRegionList.size) {
            emitter.onNext(offlineMaps)
        }
    } else {
        emitter.onNext(listOf())
    }
}

onNext这里甚至不会发生,因为状态回调是异步的。我怎样才能使onNext 等待回调然后触发。还有n个状态回调,其中n是列表中的项目数。

0 个答案:

没有答案