GitLiveApp / firebase-kotlin-sdk

A Kotlin-first SDK for Firebase
https://gitliveapp.github.io/firebase-kotlin-sdk/
Apache License 2.0
1.17k stars 155 forks source link

set/add functions don't return when no internet connection - Firestore #631

Closed stevdza-san closed 2 months ago

stevdza-san commented 2 months ago

Here's an example of one of my functions, for updating a vehicle object in Firebase. When there is an internet connection, the function properly returns a RequestState object.

override suspend fun updateVehicle(vehicle: FirebaseVehicle): RequestState<Unit> {
        return try {
            val userId = getCurrentUserId()
            val specialCharsNotAllowed = getString(Res.string.special_chars_not_allowed)
            val userNotFound = getString(Res.string.user_not_found)

            if (userId != null) {
                if (!containsSpecialCharacters(vehicle.plateNumber)) {
                        val database = Firebase.firestore
                        database.collection(collectionPath = "vehicles")
                            .document(documentPath = vehicle._id)
                            .set(vehicle.copy(ownerId = userId))
                        RequestState.Success(Unit)
                } else {
                    RequestState.Error(message = specialCharsNotAllowed)
                }
            } else {
                RequestState.Error(message = userNotFound)
            }
        } catch (e: Exception) {
            RequestState.Error(message = e.message.toString())
        }
    }

However, when I trigger this same function when there is no an internet connection, then this function never returns the above-mentioned RequestState (Even though the operation was successful). It just prints in the log, and my function doesn't catch any exception actually. Am I doing something wrong?


Stream closed with status: Status{code=UNAVAILABLE, description=Unable to resolve host firestore.googleapis.com, cause=java.lang.RuntimeException: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
                                                                    at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:223)
                                                                    at io.grpc.internal.DnsNameResolver.doResolve(DnsNameResolver.java:282)
                                                                    at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:318)
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
                                                                    at java.lang.Thread.run(Thread.java:1012)
                                                                Caused by: java.net.UnknownHostException: Unable to resolve host "firestore.googleapis.com": No address associated with hostname
                                                                    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:124)
                                                                    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
                                                                    at java.net.InetAddress.getAllByName(InetAddress.java:1152)
                                                                    at io.grpc.internal.DnsNameResolver$JdkAddressResolver.resolveAddress(DnsNameResolver.java:632)
                                                                    at io.grpc.internal.DnsNameResolver.resolveAddresses(DnsNameResolver.java:219)
                                                                    at io.grpc.internal.DnsNameResolver.doResolve(DnsNameResolver.java:282) 
                                                                    at io.grpc.internal.DnsNameResolver$Resolve.run(DnsNameResolver.java:318) 
                                                                    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
                                                                    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644) 
                                                                    at java.lang.Thread.run(Thread.java:1012) 
                                                                }.```
nbransby commented 2 months ago

The function will return when the write finishes - as you have offline persistence enabled it will write to the local database but won't finish until you go back online, this is the standard firestore behaviour of the native sdks

stevdza-san commented 2 months ago

Hmm, so what's the best solution here? I don't think adding withTimeout {} seems like a good approach, but I don't see any other?

nbransby commented 2 months ago

Either disable offline persistence and it will fail and throw on no network or keep it enabled and consider the value set and trust it will sync to the server once the client is back online