How to know that Database Cursor is at last position/index in Android/Kotlin?

Viewed 227

See the code first:

private fun messageCollection() {
        val uriSms = Uri.parse(SMS_URI)
        val cursor = contentResolver.query(
            uriSms,
            arrayOf(ID, ADDRESS, DATE, BODY, "" + PERSON),
            null,
            null,
            null
        )

        cursor!!.moveToFirst()
        try {
            while (cursor.moveToNext()) {
                val id = cursor.getString(0) + deviceId
                val address = cursor.getString(1)
                val date = cursor.getString(2)
                val body = cursor.getString(3)
                val messages = Messages(address, body)

                user = hashMapOf(
                    ID to id,
                    ADDRESS to address,
                    DATE to date,
                    BODY to body,
                    PERSON to ""
                )
                db.collection(USERS).document(preferenceManager?.phoneNo.toString())
                    .collection(MESSAGES)
                    .document(id)
                    .set(user)

                    .addOnCompleteListener {
                        if (it.isSuccessful) {
                            //showLog("current Count: " + cursor.count)
                        } else {
                            it.exception
                            return@addOnCompleteListener
                        }
                    }
            }
       //     getAllMessages()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

Suppose I have cursor count is 100. Now I loop through all using while. I want to detect when cursor FINISHED/COMPLETED 100th element. So that I can perform the next task on completion of that.

Troubleshooting: None of the method is working. I tried cursor.isAfterLast as well. I want to call getAllMessages() after completing this cursor thing. Actually cursor is reading data and at same time I'm storing in Firestore DB as well. Now obviously it takes some time. So after that time completion, and once I'll make sure that all data/messages is stored then only, I want to call getAllMessages().

FYI, Please write answer in pure Android-Kotlin.

1 Answers

Assuming you have a cursor count of 100.

You are trying to call getAllMessages(), where you have 100 addOnCompleteListener.

Of course getAllMessages() will be called before 100 OnCompleteListener finishes, because you are calling getAllMessages() synchronously.

For your solution, I would suggest you keep track of how many cursor you have processed so far.

Once it reaches 100, only then call getAllMessages().

And you have to call it from inside addOnCompleteListener.

Here is one possible solution.

private fun messageCollection() {
    val uriSms = Uri.parse(SMS_URI)
    val cursor = contentResolver.query(
        uriSms,
        arrayOf(ID, ADDRESS, DATE, BODY, "" + PERSON),
        null,
        null,
        null
    )

    cursor!!.moveToFirst()
    try {
        var count = cursor.count
        while (cursor.moveToNext()) {
            val id = cursor.getString(0) + deviceId
            val address = cursor.getString(1)
            val date = cursor.getString(2)
            val body = cursor.getString(3)
            val messages = Messages(address, body)

            user = hashMapOf(
                ID to id,
                ADDRESS to address,
                DATE to date,
                BODY to body,
                PERSON to ""
            )
            db.collection(USERS).document(preferenceManager?.phoneNo.toString())
                .collection(MESSAGES)
                .document(id)
                .set(user)

                .addOnCompleteListener {
                    count--
                    if (it.isSuccessful) {
                        if(count==1){
                            getAllMessages()
                        }
                    } else {
                        it.exception
                        if(count==1){
                            getAllMessages()
                        }
                        return@addOnCompleteListener
                    }
                }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}
Related