All Downloads are FREE. Search and download functionalities are using the official Maven repository.

iosMain.com.estiven.firebase_firestore.CollectionReference.kt Maven / Gradle / Ivy

Go to download

FirebaseKMM is a Firebase Extension that supports cross-platform projects also based on Firebase Kotlin SDK, allowing you to directly from iOS and Android.

There is a newer version: 0.7.15
Show newest version
package com.estiven.firebase_firestore

import cocoapods.FirebaseFirestore.FIRCollectionReference
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.callbackFlow

actual class CollectionReference(private val iOS: FIRCollectionReference) : Query(iOS) {
    actual val id
        get() = iOS.collectionID
    actual val parent: DocumentReference?
        get() = iOS.parent?.let { DocumentReference(it) }
    actual val path
        get() = iOS.path
    actual val firestore
        get() = FirebaseFirestore(iOS.firestore)

    actual fun document() = DocumentReference(iOS.documentWithAutoID())
    actual fun document(documentPath: String) =
        DocumentReference(iOS.documentWithPath(documentPath))


    actual suspend fun get() =
        QuerySnapshot(awaitResult { iOS.getDocumentsWithCompletion(it) })

    @Suppress("UNCHECKED_CAST")
    actual suspend fun add(data: Any) =
        DocumentReference(await {
            iOS.addDocumentWithData(
                data as Map,
                it
            )
        })

    actual fun snapshots(metadataChanges: MetadataChanges) = callbackFlow {
        val listener =
            iOS.addSnapshotListenerWithIncludeMetadataChanges(
                metadataChanges == MetadataChanges.INCLUDE
            ) { firQuerySnapshot, nsError ->
                if (nsError != null) close(Exception(nsError.toString()))
                if (firQuerySnapshot != null) trySend(QuerySnapshot(firQuerySnapshot))
            }
        awaitClose { listener.remove() }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy