Exception Kotlin calbbackFlow捕获运算符未捕获异常
我正在使用回调流来观察Firestore文档。当使用应用程序的其他人移动或删除正在监视的文档时,我的流需要抛出ResourceNotFoundException。下面是我的流程代码Exception Kotlin calbbackFlow捕获运算符未捕获异常,exception,kotlin-flow,Exception,Kotlin Flow,我正在使用回调流来观察Firestore文档。当使用应用程序的其他人移动或删除正在监视的文档时,我的流需要抛出ResourceNotFoundException。下面是我的流程代码 fun observeDocument(collectionId: String, documentId: String) = callbackFlow { database.collection(collectionId).document(documentId) .addSnapsho
fun observeDocument(collectionId: String, documentId: String) = callbackFlow {
database.collection(collectionId).document(documentId)
.addSnapshotListener { documentSnapshot, firebaseFireStoreException ->
if (firebaseFireStoreException != null)
throw firebaseFireStoreException
if (documentSnapshot == null || !documentSnapshot.exists()) {
throw ResourceNotFoundException("")
}
try {
offer(documentSnapshot.toObject(PrintOrder::class.java))
} catch (e: Exception) {
}
}
awaitClose { }
}
我使用以下代码在ViewModel中收集上述流
viewModelScope.launch {
repository.observeDocument(collectionId, documentId)
.catch {e->
_loadedPrintOrder.value = LoadingStatus.Error(e)
}
.onStart {
_loadedPrintOrder.value = LoadingStatus.Loading(application.getString(R.string.one_moment_please))
}
.collect {
_loadedPrintOrder.value = LoadingStatus.Success(it!!)
}
}
现在,问题是catch操作符没有捕获从流抛出的异常(ResourceNotFoundException)。我甚至尝试过将流集合代码包装在try-catch块中。即使try-catch块也无法捕获异常,应用程序也会崩溃
如何捕获流收集期间从callBackFlow抛出的异常