简体   繁体   中英

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

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 .

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

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()}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM