Nikola Milovic
02/16/2020, 6:55 PMoverride fun loadDrillTypes(): Observable<ArrayList<DrillsType>> {
val list = ArrayList<DrillsType>()
firebaseFirestore.collection("drilltypes")
.get()
.addOnSuccessListener { documents ->
for (document in documents) {
val doc = document.toObject(DrillsType::class.java)
list.add(doc)
}
}
.addOnFailureListener { exception ->
Log.w("TAG", "Error getting documents: ", exception)
}
return Observable.just(list) // delay(2, TimeUnit.SECONDS)
}
I get an empty ArrayList
compositeDisposable += repository.loadDrillTypes()
.subscribeWith(object : DisposableObserver<ArrayList<DrillsType>>() {
override fun onError(e: Throwable) {
//if some error happens in our data layer our app will not crash, we will
// get error here
}
override fun onNext(data: ArrayList<DrillsType>) {
Log.d("TAG", data.toString())
}
override fun onComplete() {
Log.d("TAG", "COMPLETE")
}
})
}
But if i add the delay(2, seconds) then I get the normal result that I need... I am 100% sure there is a way around this without having it to be delayed. Or is the delay a standard practice?kioba
02/16/2020, 6:59 PMJust
immediately fires the list and does not wait for the firestore to finish.
OnSuccess/onFaliure is a callback from firestore. It doesn't block the execution of the functionNikola Milovic
02/16/2020, 7:03 PMkioba
02/16/2020, 7:12 PMSingle.create { emitter ->
val storageTask = firebaseFirestore.collection("drilltypes")
.get()
.addOnSuccessListener { documents ->
val success = documents.map { document.toObject(DrillsType::class.java) }
emitter.onSuccess(success)
}
.addOnFailureListener { exception ->
if (emitter.isDisposed().not())
emitter.onError(exception)
Log.w("TAG", "Error getting documents: ", exception)
}
emitter.setCancellable{
storageTask.cancel()
}
}
Nikola Milovic
02/16/2020, 7:15 PMkioba
02/16/2020, 7:15 PM