Я предоставлю вам важную часть моего вспомогательного класса, viewModel в обоих случаях. И соответствующее сообщение logcat для обоих случаев.
Случай 1 (получение пустого или нулевого значения)->
- ViewModel (важная часть)->
Код: Выделить всё
val firestoreHelperForToken = FirestoreHelperForToken() private val _getAllNames = MutableLiveData() val getAllNames : LiveData get() = _getAllNames init { //subscribe to a topic to broadcast the message viewModelScope.launch { firestoreHelperForToken.fetchAllNames.collect { namesList -> _getAllNames.postValue(namesList) // Log inside the collect block to ensure that the data is fetched Log.d("ChatViewModel", "List from Firestore: $namesList") // Small delay to ensure data sync if needed delay(100) // Check if list is not null or empty and log it if (!namesList.isNullOrEmpty()) { Log.d("ChatViewModel", "Valid list received: $namesList") } else { Log.d("ChatViewModel", "Empty list received.") } } // Log outside of the collect block after it finishes fetching Log.d("ChatViewModel", "List outside the collect block: ${getAllNames.value}") }} - HelperClass (важная часть)->
Код: Выделить всё
class FirestoreHelperForToken {
private val TAG = "FirestoreHelperForToken"
private val _db = Firebase.firestore
val fetchAllNames: Flow[*]> = channelFlow {
Log.d(TAG, "Setting up addSnapshotListener")
_db.collection("tokens")
.addSnapshotListener { snapshot, e ->
if (e != null) {
Log.w(TAG, "Listen failed with error: $e")
return@addSnapshotListener
}
if (snapshot == null || snapshot.isEmpty) {
Log.d(TAG, "Snapshot is null or empty.")
trySend(emptyList()) // Emit empty list if no documents are found
return@addSnapshotListener
}
val namesList = mutableListOf()
Log.d(TAG, "Snapshot size: ${snapshot.size()}")
for (document in snapshot.documents) {
val name = document.getString("name")
name?.let {
namesList.add(it)
}
}
try {
Log.d(TAG, "Names list: $namesList")
trySend(namesList) // Emit the list of names
} catch (sendError: Exception) {
Log.e(TAG, "Error while sending names list: ${sendError.message}")
}
}
}.flowOn(Dispatchers.IO)
}
Код: Выделить всё
2024-10-06 10:57:26.586 1389-1422 FirestoreHelperForToken com.example.fcmpractice D Setting up addSnapshotListener
2024-10-06 10:57:27.296 1389-1389 ChatViewModel com.example.fcmpractice D List outside the collect block: null
2024-10-06 10:57:27.299 1389-1389 FirestoreHelperForToken com.example.fcmpractice D Snapshot size: 17
2024-10-06 10:57:27.312 1389-1389 FirestoreHelperForToken com.example.fcmpractice D Names list: [mona, abhijit, mahesh, raka, ritu, ranna, jodha, biru, madhav, babu, rajat, sujal, mauni, meme, raghav, mamu, nammy]
- HelperClass->
2. Модель просмотра ->
Код: Выделить всё
fun getNamesFlow() : Flow = callbackFlow { val collectionRef = _db.collection("tokens") val listner = collectionRef.addSnapshotListener { snapshot, error -> if (error != null) { close(error) return@addSnapshotListener } if (snapshot != null && !snapshot.isEmpty){ val nameList = snapshot.documents.map { it.getString("name") ?: "" } trySend(nameList) }else{ trySend(emptyList()) } } awaitClose { listner.remove() } }Код: Выделить всё
viewModelScope.launch { Log.d("ChatViewModel", "List outside the collect block: ${getAllNames.value}") firestoreHelperForToken.getNamesFlow() .collect { namesList -> _getAllNames.postValue(namesList) // Log inside the collect block to ensure that the data is fetched Log.d("ChatViewModel", "List from Firestore: $namesList") }
Код: Выделить всё
2024-10-06 11:03:12.399 1851-1851 ChatViewModel com.example.fcmpractice D List outside the collect block: null
2024-10-06 11:03:13.103 1851-1851 ChatViewModel com.example.fcmpractice D List from Firestore: [mona, abhijit, mahesh, raka, ritu, ranna, jodha, biru, madhav, babu, rajat, sujal, mauni, meme, raghav, mamu, nammy]
Подробнее здесь: https://stackoverflow.com/questions/790 ... in-other-c