问题
I have an issue with Firebase's Firestore in my app, based on a MVVM architecture. When I try to get an object from a document reference, the addOnCompleteListener never succed (neither the other listeners). Here is my code of ItemRepo.kt in which I call the get method to my document reference.
NOTE: myDocRef is used in the init of the viewModel (called by the getMyItems method that returns a Query objcet that I observe with a snapshotListener) and is ordered by those two fields. I've tried to use directly that reference and also a new one, local in the function getMyItem
private val myDocRef = db.collection("users").document(userAuth!!.uid).collection("myitems")
...
fun getMyItems() : Query {
return myDocRef.orderBy("title", Query.Direction.ASCENDING)
.orderBy("expiryDate", Query.Direction.DESCENDING)
}
...
fun getMyItem(adId : String) : Item {
var item : Item? = null
/*val docRef = db.collection("users").document(userAuth!!.uid)
.collection("myitems").document(adId)*/
/*the previous code was just a try
**to use non ordered collection
* no success!*/
myDocRef.document(adId)
.get().addOnCompleteListener { task ->
if (task.isSuccessful) {
item = task.result!!.toObject(Item::class.java)!!
} else {
Log.d("firestore","get KO!")
}
}
return item!!
}
I've also tried to write manually the adId to match one of the documents in that collection, without positive results...
I need the return value of that function (the object contained in that document) outside but I'm stuck here.
NOTE: I have another repository in which I manage the user document in the same way and everything works perfectly
EDITED: maybe could it happen because I already have a listener on the collection which I want to get only one document from?
来源:https://stackoverflow.com/questions/61843756/firestore-metadata-get-method-doesnt-succed