pageList反应式库(Observable <List <Model >>)不起作用

时间:2019-07-19 09:14:00

标签: kotlin reactive rx-kotlin

我有recyleviewfirestore,并且我正在使用pagingLibray。我有问题,我的代码无法通过从firestoreaddsnapshotlistener方法中获取数据来返回Observable。

我检查了可观察的日志,但没有出现

我使用引用 thisthis

存储库

suspend  fun getBalasan(
    pageSize: Int,
    loadBefore: String? = null,
    loadAfter: String? = null

):Observable<List<RealtimeDiskusi>>{
    var query = db.collection("post/$postId/diskusi/")
        .orderBy("createAt", Query.Direction.DESCENDING).limit(pageSize.toLong())

    Log.d(TAG,"Runingg....  $postId")
    Log.d(TAG,"Runingg query ....  $query")
    loadBefore?.let {
        val item = db.collection("post/$postId/diskusi/").document(it)
            .get()
            .await()

        query = query.endBefore(item)
    }

    loadAfter?.let {
        val item = db.collection("post/$postId/diskusi/").document(it).get().await()

        query = query.startAfter(item)
    }
    return  Observable.create<List<RealtimeDiskusi>> {
        emitter ->
        query.addSnapshotListener { snapshot, exception ->

            snapshot?.map {
                RealtimeDiskusi(
                    it.id,
                    getRecord(it.id)
                )

            }
        }

    }


}



private fun getRecord(id: String): Observable<Diskusi> {
    Log.d(TAG,"item id record....  $id")
    return Observable.create<Diskusi> { emitter ->
        db.collection("post/$postId/diskusi").document(id).addSnapshotListener { snapshot, exception ->
            if (exception != null) {
            } else if (snapshot != null && snapshot.exists()) {
                emitter.onNext(
                    snapshot.toObject(Diskusi::class.java)
                        ?: throw IllegalArgumentException()
                )
            } else {
                Log.d("Respository", "Current data: null")
            }
        }
    }
}

数据源

        uiScope.launch {
        val items = repository.getBalasan(params.requestedLoadSize)
        items.observeOn(Schedulers.io()).subscribeOn(AndroidSchedulers.mainThread())
            .subscribe {
                Log.d(TAG , it.toString())  //not Working
                callback.onResult(it)
            }

    }

0 个答案:

没有答案