我有recyleview
和firestore
,并且我正在使用pagingLibray
。我有问题,我的代码无法通过从firestore
和addsnapshotlistener
方法中获取数据来返回Observable。
我检查了可观察的日志,但没有出现
存储库
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)
}
}