Simon
Simon

Reputation: 75

Firestore live update using Kotlin Flow

I want to implement system with live updates (similar to onSnapshotListener). I heard that this can be done with Kotlin Flow.

Thats my function from repository.

 suspend fun getList(groupId: String): Flow<List<Product>> = flow {
        val myList = mutableListOf<Product>()
        db.collection("group")
            .document(groupId)
            .collection("Objects")
            .addSnapshotListener { querySnapshot: QuerySnapshot?,
                                   e: FirebaseFirestoreException? ->
                if (e != null) {}
                querySnapshot?.forEach {
                    val singleProduct = it.toObject(Product::class.java)
                    singleProduct.productId = it.id
                    myList.add(singleProduct)
                }
            }
       emit(myList)
    }

And my ViewModel

class ListViewModel: ViewModel() {

private val repository = FirebaseRepository()
private var _products = MutableLiveData<List<Product>>()
val products: LiveData<List<Product>> get() = _produkty


init {
    viewModelScope.launch(Dispatchers.Main){
        repository.getList("xGRWy21hwQ7yuBGIJtnA")
            .collect { items ->
                _products.value = items
            }
    }
}

What do I need to change to make it work? I know data is loaded asynchronously and it doesn't currently work (the list I emit is empty).

Upvotes: 4

Views: 3840

Answers (3)

Ros&#225;rio P. Fernandes
Ros&#225;rio P. Fernandes

Reputation: 11316

Starting in firestore-ktx:24.3.0, you can use the Query.snapshots() Kotlin flow to get realtime updates:

suspend fun getList(groupId: String): Flow<List<Product>> {
    return db.collection("group")
            .document(groupId)
            .collection("Objects")
            .snapshots().map { querySnapshot -> querySnapshot.toObjects()}
}

Upvotes: 10

c_idle
c_idle

Reputation: 1534

As of 2 days ago, firestore has this functionality provided out of the box: https://github.com/firebase/firebase-android-sdk/pull/1252/

Upvotes: 0

Arpit Shukla
Arpit Shukla

Reputation: 10493

You can use this extension function that I use in my projects:

fun Query.snapshotFlow(): Flow<QuerySnapshot> = callbackFlow {
    val listenerRegistration = addSnapshotListener { value, error ->
        if (error != null) {
            close()
            return@addSnapshotListener
        }
        if (value != null)
            trySend(value)
    }
    awaitClose {
        listenerRegistration.remove()
    }
}

It uses the callbackFlow builder to create a new flow instance.

Usage:

fun getList(groupId: String): Flow<List<Product>> {
    return db.collection("group")
        .document(groupId)
        .collection("Objects")
        .snapshotFlow()
        .map { querySnapshot ->
            querySnapshot.documents.map { it.toObject<Product>() }
         }
}

Note that you don't need to mark getList as suspend.

Upvotes: 9

Related Questions