0

I'm trying to retrieve data from realtime database with the code below. I want to display those data in textview but it didn't show up unfortunately in logcat I got error message as shown below. I think the code is ok but it's maybe sth wrong with firebase or thing related with firebase. please kindly help !

private fun getStudentData(){
    database = FirebaseDatabase.getInstance().getReference("users")
    database.child(uid).get().addOnSuccessListener {
        val studentName = it.child("name").value
        val studentBio = it.child("bio").value

        binding.tvStudentName.text = studentName.toString()
        binding.tvStudentBio.text = studentBio.toString()
    }.addOnFailureListener{
        Log.e("firebase", "Error getting data", it)
    }
}
2021-10-24 21:59:22.498 30028-30028/com.example.amazontutoringcenter E/firebase: Error getting data
    java.lang.Exception: Client is offline
        at com.google.firebase.database.connection.PersistentConnectionImpl.lambda$get$1$PersistentConnectionImpl(PersistentConnectionImpl.java:441)
        at com.google.firebase.database.connection.PersistentConnectionImpl$$ExternalSyntheticLambda4.run(Unknown Source:8)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:301)
        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:929)

enter image description here

Phearum
  • 53
  • 1
  • 8
  • What did you expect the code to do? Why do you think the result you get is incorrect? – Frank van Puffelen Oct 24 '21 at 15:16
  • Have you checked your permission rules ? Does your app is allowed to read that collection? – Kathan Patel Oct 24 '21 at 15:30
  • @FrankvanPuffelen I was trying to display some information of users from firebase realtime database in textview but It didn't display anything and logcat it showed that error. –  Phearum Oct 25 '21 at 01:03
  • @KathanPatel you refered to firebase rule that we allow users to write and read data right ? I already made them true. –  Phearum Oct 25 '21 at 01:05
  • Yes, I was referring to the same. Have you checked what exception it is throwing in addOnFailureListener ? – Kathan Patel Oct 25 '21 at 01:58
  • @KathanPatel the exception says client is offline but it's not. –  Phearum Oct 25 '21 at 02:42
  • @Phearum How do you check if the user is online? Also, show us your database structure. – Alex Mamo Oct 25 '21 at 13:18
  • @AlexMamo for online user I thought when they're connected to the internet and be able to log in is considered an online user. Maybe there are some methods to check off/online users you may let me know. about the database structure, I already updated in my question above. Thanks! –  Phearum Oct 26 '21 at 00:15
  • Try to use this [solution](https://stackoverflow.com/questions/68559694/can-you-check-for-firebase-server-problems-in-android-code) and tell me if it works for you to check the internet connectivity. – Alex Mamo Oct 26 '21 at 06:31

2 Answers2

1

I came across this error when working with React Native/Expo(v42.0.1), Firebase(v9.4.0), and Realtime DB. The code would work flawlessly on Android VMs...

const queryRef = query(ref(database, "..."));
get(queryRef)
  .then((snapshot) => {
    if (snapshot.exists()) {...} 
    else {...}
  })
  .catch((err) => {...});

but would fail with this error on physical Android devices...

Error: Client is offline.
at node_modules\@firebase\database\dist\exp\index.esm2017.js:11172:22 in repo.server_.get.then$argument_1
at node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in tryCallOne
at node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass
at node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
at node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue

If I called the get() again after it initially failed, it would work! So I went back and read through the firebase docs. I eventually landed on the Enable Offline Capabilities - Kotlin page. There is a section on Detecting Connection State - Kotlin. After wrapping my code in the success if statement, my problem was solved!

val connectedRef = Firebase.database.getReference(".info/connected")
connectedRef.addValueEventListener(object : ValueEventListener {
    override fun onDataChange(snapshot: DataSnapshot) {
        val connected = snapshot.getValue(Boolean::class.java) ?: false
        if (connected) {
            Log.d(TAG, "connected")

            //DB is ready for get() call 

        } else {
            Log.d(TAG, "not connected")
        }
    }

    override fun onCancelled(error: DatabaseError) {
        Log.w(TAG, "Listener was cancelled")
    }
})

Note: I noticed that when using this approach, "not connected" is always called before "connected". This has made error handling interesting.

0

It wasn't working for me because I didn't specify server Url when getting database Instance. Besides this error I was getting this in console :

Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Database lives in a different region. Please change your database URL to /my database url here/

To fix it I just specified server like that:

 FirebaseDatabase.getInstance("YOUR_URL_HERE")

The parameter "YOUR_URL_HERE" can be found in your firebase console. For example:

screenshot

Skomorow
  • 3
  • 5