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.