0

Im trying to get the number of items that are in a list in my realtime database so that i can use the number to display how many cells there are but i can't find a way to do this as the code does not finish executing before the number is returned and i've tried to use a call back but its not working because it tells me to return a unit and i need to return a int

Here is my code:

interface GetCountCallback {
    fun onCallback(list: ArrayList<String>)
}

override fun getItemCount(): Int {

    var number = 0
    getCount(object:GetCountCallback {
        override fun onCallback(list: ArrayList<String>) {
            number = list.count()
        }
    })
    return number
}

private fun getCount(myCallback: GetCountCallback) {

    val ref = dbSavedStories.child(currentUid!!)

    ref.addListenerForSingleValueEvent(object : ValueEventListener {
        override fun onDataChange(snapshot: DataSnapshot) {
            for (childSnapshot in snapshot.children) {
                val key = childSnapshot.key
                list.add(key.toString())
            }
            myCallback.onCallback(list)
        }
        override fun onCancelled(error: DatabaseError) {
            TODO("Not yet implemented")
        }
    })
}
es915
  • 137
  • 3
  • 11
  • You can't return something that is loaded asynchronously. In fact, the whole reason you have `myCallback.onCallback(list)` is precisely that. If you run your code in a debugger and set breakpoints, you'll see that `return number` runs before `number = list.count()` in your code. I recommend re-reading https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 and https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value – Frank van Puffelen Dec 05 '20 at 15:50
  • Yes i know whats happening and why it returns before, i've read the above question and am still not understanding how i would do it in my situation. i have used this in other places to get values but now i'm trying to get how many values are in the data base. Can you help me fix it by giving advise please? – es915 Dec 05 '20 at 16:11
  • I can't do more that what I said in my first comment, what the linked questions explain, and what you already do for getting the value out of `getCount`. You can't return something that is loaded asynchronously: your `return number` runs before `number = list.count()` and you'll need to use a callback to fix that. – Frank van Puffelen Dec 05 '20 at 16:27
  • But i have used a call back. Have i put it in the wrong place? – es915 Dec 05 '20 at 16:34
  • You're using a callback to get the value out of `getCount`, but not to get the value out of `getItemCount`. – Frank van Puffelen Dec 05 '20 at 17:05
  • Yes because im trying to get the number from the getCount method and return it in the getItemCount method as the getItemCount will return the number of items to be generated. – es915 Dec 05 '20 at 20:02
  • You can't **return** something that is asynchronously determined. Just like `getCount` can't **return** `list`, your `getItemCount` also can't return the count. Similarly to your `GetCountCallback.onCallback` you'll need a custom callback that passes the count as an argument. It's really the same as in the answer I linked, and that you copied for `getCount` already. – Frank van Puffelen Dec 05 '20 at 21:13
  • So is there no way to get the number of items? – es915 Dec 05 '20 at 21:39

0 Answers0