Home > Software design >  Firestore database read count
Firestore database read count

Time:03-10

In my app i use the snapshot listener connected to a collection in the firestore database.

private fun getMessages() {
    Firebase.firestore.collection(Constants.MESSAGES)
        .orderBy(Constants.SENT_ON)
        .addSnapshotListener { value, e ->
            if (e != null) {
                Log.w(Constants.TAG, "Listen failed.", e)
                return@addSnapshotListener
            }

            val list = emptyList<Map<String, Any>>().toMutableList()

            if (value != null) {
                for (doc in value) {
                    val data = doc.data

                    list.add(data)
                }

            }

            updateMessages(list)
        }
}

In the collection there are 1000 items. Is my code snippet the most efficient one ? What i want is that a new app user the first time he connects to the database uses 1000 reads. After that due to the snappshot listener only the new documents in the collection.

CodePudding user response:

The code looks fine at first glance to me.

The only thing I'd do is add a limit to the query for what you think is a reasonable maximum that any user may want to see.

What you can consider is implementing pagination or endless scrolling with query cursors, and only load the even smaller subset that the user can actually see on their device screen.

  • Related