Home > Blockchain >  Firestore live update using Kotlin Flow
Firestore live update using Kotlin Flow

Time:05-30

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).

CodePudding user response:

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.

  • Related