android – Mongo Stitch how to make sure code runs in correct order?-ThrowExceptions

Exception or error:

When I run the below code it executes in the order of 0 3 1 2 (as denoted by Log.d(“partorder”)) how do I ensure that the code runs in the order 0 1 2 3? As otherwise fr is not correctly returned.

    val stitchAppClient = Stitch.getDefaultAppClient()
    var fr : ArrayList<String> = ArrayList<String>()
    Log.d("partorder", "0")

    // Call Database
    stitchAppClient.auth.loginWithCredential(AnonymousCredential()).addOnSuccessListener {

        val client = stitchAppClient.getServiceClient(RemoteMongoClient.factory, "mongodb-atlas")
        val coll = client.getDatabase("appdata").getCollection("categories")
        val query = coll.find()
        val result = mutableListOf<Document>()

        Log.d("partorder", "1")

        fr = ArrayList<String>()

        query.into(result).addOnSuccessListener {
            Log.d("partorder", "2")
            result.forEach {
                fr.add(it["categoryTitle"] as String)
            }
        }
    }

    Log.d("partorder", "3")
    return fr
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *