0

I want to handle errors during fetching data about users. When there is no internet connection it works great but when there is a problem like for e.g. there is no searching object in my Firestore database function doesn't throw any exception. How I can detect timeout or other errors?

@ExperimentalCoroutinesApi override suspend fun getUserData(): Flow<FirebaseEventResponse> = callbackFlow { try { val userId = firebaseAuth.currentUser?.uid.toString() firebaseInstance.collection(userCollection).document(userId + "loremIpsu").get().addOnCompleteListener { if (it.isSuccessful) { if (it.result.metadata.isFromCache) { trySend(ExceptionOccurred.NetworkEvent(Exception())) } else { trySend(SuccessDocument(it.result)) } } else { trySend(ExceptionOccurred.Event(Exception())) } } } catch (e: Exception) { trySend(Event(e)) } awaitClose { this.cancel() } } 

1 Answer 1

1

When there is no internet connection it works great.

That's the expected behavior since, for Android, Firestore has its own offline persistence mechanism enabled by default.

When there is a problem like for e.g. there is no searching object in my Firestore database function doesn't throw any exception.

If you are creating a document reference that points to a particular document, and if you call get() and the document doesn't exist, it doesn't mand that an Exception will be thrown. And it makes sense since the absence of a document should not raise an Exception. However, if for example, that request fails due to improper security rules, then it definitely makes sense to raise an Exception.

How I can detect timeout or other errors?

Please note that read operations in Firestore never time out. Why? Because the operations never fail due to loss of network. If there is a loss of network (there is no network connection on the user device), neither the OnFailureListener nor the OnCanceledListener gets triggered. Behind the scenes, Firestore SDK tries to reconnect until the devices regain connectivity. Don't think of Firestore reads and writes as typical input and output operations.

Other errors can be caught in the way that you already do.

Sign up to request clarification or add additional context in comments.

1 Comment

All clear. Thank you for the explanation!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.