https://kotlinlang.org logo
Title
r

Remon Shehata

05/15/2020, 1:25 AM
override fun checkOrganization(organizationId: String): Boolean {
    var isFound = false
    db.collection(FirebaseConstants.ORGANIZATION)
        .get()
        .addOnSuccessListener { querySnapshot ->
            for (document in querySnapshot) {
                Log.d(TAG, document.id)
                if (organizationId == document.id) {
                    isFound = true
                    
                }
            }
        }
        .addOnFailureListener { exception ->
            Log.w(TAG, "Error getting documents.", exception)
        }

    return isFound
}
I want to return from the method after the loop is completed. how to achieve that?
:stackoverflow: 2
o

OG

05/17/2020, 7:56 PM
Idk how this whole thing is used but what comes to mind is either passing in a callback interface to this class that is overriding
checkOrganization
and invoking the callback in the
addOnSuccessListener
. Another alternative would be changing this interface (if it's internal) and using coroutines so you can return the result asynchronously.
So a.) Use coroutines and mark
checkOrganization
a suspend function so you can return the result asynchronously. b.) Use traditional callbacks to pass the result at a later time (the end of the for loop in
addOnSuccessListener