Fawwaz Ali
Fawwaz Ali

Reputation: 57

Data retrieving from Database Crashes when in MVVM with the Error Could not Connect To Firestore

I am trying to call data from Firebase and map it to data class while using MVVM but Its showing me errors saying failed to Connect

The code of Repository Implementation:

    override suspend fun uploaded_docs_retrival(): ArrayList<String> {
    return try {
        val result = Firebase.firestore.collection("users").whereEqualTo("UID", "84HMm1MJx8X8G33i7sxqUKQLzTn2").get()
            .await()
        result.first().toObject(docretreving::class.java).Docs
    } catch (e: Exception) {
        Log.e("TAG", "Failed to upload: ${e.message.orEmpty()}")
        ArrayList()
    }
}

Code of View Model:-

     val livedata : MutableLiveData<ArrayList<String>> by lazy 
    MutableLiveData<ArrayList<String>>()

    fun uploaded_docs_retrival() {
      viewModelScope.launch(Dispatchers.IO){
          livedata.postValue(repository.uploaded_docs_retrival())

      }

   }

I am trying to call the function from View Model and the error I am seeing is:

W/Firestore: (24.0.1) [WatchStream]: (297224f) Stream closed with status: Status{code=INTERNAL, description=error in frame handler, cause=java.lang.NoSuchMethodError: No interface method getBuffer()Lokio/Buffer; in class Lokio/BufferedSource; or its super classes (declaration of 'okio.BufferedSource' appears in /data/app/~~zy8Sr4ZK3BufSuP5iCevdQ==/com.example.alliaiseV1-OysKU8xYFaKtBxHIS_ndvw==/base.apk!classes15.dex)
    at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.data(OkHttpClientTransport.java:1144)
    at io.grpc.okhttp.internal.framed.Http2$Reader.readData(Http2.java:234)
    at io.grpc.okhttp.internal.framed.Http2$Reader.nextFrame(Http2.java:147)
    at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.run(OkHttpClientTransport.java:1103)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
}.
 W/Firestore: (24.0.1) [OnlineStateTracker]: Could not reach Cloud Firestore backend. 
 Connection failed 1 times. Most recent error: Status{code=INTERNAL, description=error 
 in frame handler, cause=java.lang.NoSuchMethodError: No interface method 
 getBuffer()Lokio/Buffer; in class Lokio/BufferedSource; or its super classes 
 (declaration of 'okio.BufferedSource' appears in 
 /data/app/~~zy8Sr4ZK3BufSuP5iCevdQ==/com.example.alliaiseV1- 
 OysKU8xYFaKtBxHIS_ndvw==/base.apk!classes15.dex)
    at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.data(OkHttpClientTransport.java:1144)
    at io.grpc.okhttp.internal.framed.Http2$Reader.readData(Http2.java:234)
    at io.grpc.okhttp.internal.framed.Http2$Reader.nextFrame(Http2.java:147)
    at io.grpc.okhttp.OkHttpClientTransport$ClientFrameHandler.run(OkHttpClientTransport.java:1103)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
    at java.lang.Thread.run(Thread.java:923)
}
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
 E/TAG: Failed to upload: Collection is empty.

The TAG in last line of error Indicates I am getting a Exception in My Repository Function Which is the First Function of this Question Mentioned Above. However if I modify the Function to below and I don't use View Model and Directly Code the Function below in the Fragment and check the Logcat it is Retrieving perfectly Altered Trying Code:

        suspend fun uploaded_docs_retrival(): ArrayList<String> {
        return try {
            val result = Firebase.firestore.collection("profiles").whereEqualTo("UID", "YHMauLouORRtrYBV2h4dHJ5A0s72").get()
                .await()
            result.first().toObject(docretreving::class.java).Docs
        } catch (e: Exception) {
            Log.e("TAG", "Failed to upload: ${e.message.orEmpty()}")
            ArrayList()
        }
    }
    lifecycleScope.launch {
       val z = uploaded_docs_retrival()
        Log.d("Cross","$z")

    }

How can the same Code work and if I use View Models and Live Data It crashes. Help would be Appreciated , Been a quite While I am stuck with this. Thanks in Advance

Upvotes: 0

Views: 625

Answers (1)

Pablo
Pablo

Reputation: 68

Looks like a library is missing. Try adding:

implementation("com.squareup.okhttp3:okhttp:4.9.3")

Upvotes: 3

Related Questions