0

I want to read from Firebase Real-time Database and get the value of total child.

I am using ListenerforSingleValueEvent() for this. The function is supposed to run before another function which will use the total child as the 'for' loop condition. But when ever I try to run the function, it is showing Error in the Logcat along with the correct result but after the completion of the second function which is supposed to use this result.

Here is the code of the function:

public void total_items() {

    Log.e(TAG, " Start of function total_items()--> ");
    Urll = FirebaseDatabase.getInstance().getReference().child("data").child("item").child(item_type);
    Urll.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Log.e(TAG, " Total items : " + dataSnapshot.getChildrenCount());
        Log.e(TAG, " Total data  : " + dataSnapshot);
        total_item = (int) dataSnapshot.getChildrenCount();
        Log.e(TAG, " Total items : " + total_item);
        }
    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, ":  Error is:  " + databaseError);
        }
         });
    Log.d(TAG, "End of function total_items()--> ");
    }

Here is the Logcat:

    1   E/Items.Class: Start of function total_items()--> 
    2   D/Items.Class: Database URL : https://Bay-Cart.firebaseio.com/data/item/winter
    3   D/Items.Class: End of function total_items()--> 
    4   D/Items.Class: run: AsyncTask: 2 
    5   D/Items.Class: onPreExecute: !! ---------->
    6   D/Items.Class: onPostExecute: !! ---------->
    7   D/Items.Class: progress 1 
    8   D/Items.Class: progress 2 
    9   D/Items.Class: run: AsyncTask: 3 
    10  D/Items.Class: onPreExecute: !! ---------->
    11  D/Items.Class: onPostExecute: !! ---------->
    12  D/Items.Class: progress 3 
    13  D/Items.Class: Item_type :: winter
    14  D/Items.Class: !!!!!! debug 3.6
    15  D/Items.Class: !!!!!! debug 3.8
    16  D/Items.Class: Item name :::::::::
    17  D/Items.Class: Item name :::::::::
    18  D/Items.Class: progress 4 
    19  W/Adreno-ES20: <core_glFinish:34>: glFinish skipped: 0
    20  W/Adreno-ES20: <core_glFinish:34>: glFinish skipped: 0
    21  D/Items.Class: onPostExecute: !! ---------->
    22  W/Adreno-ES20: <core_glFinish:34>: glFinish skipped: 0
    23  W/InputTransport: Slow Input: 153ms so far, channel 'ClientState{44af2c8 uid 10169 pid         15939} (server)' publisher ~ 
publishKeyEvent: seq=1, deviceId=4, source=0x101, action=0x0, flags=0x8, keyCode=24, scanCode=115, metaState=0x0, 
repeatCount=0,downTime=10638911000000, eventTime=10638911000000
    24  I/zygote64: Do full code cache collection, code=1006KB, data=669KB
    25  I/zygote64: After code cache collection, code=999KB, data=584KB
    26  E/Items.Class:  Total items : 2
    27  E/Items.Class:  Total data  : DataSnapshot { <--!!some text!!--> }
    28  E/Items.Class:  Total items : 2
    29  W/Adreno-ES20: <core_glFinish:34>: glFinish skipped: 0

In the above Logcat line 1 Shows the starting of the function with an Error and line 3 shows the end of it. After that other AsyncTasks are running in an sequence till line 25. But line 26 to line 28 shows the correct output of the above function along with an error code And also these outputs should be above "AsyncTask: 2" in line 4.

I am using 4 AsyncTask tasks in sequence and calling using runOnUiThread() on onPostExecute() to call another AsyncTask.

Things I have tried so far:

I have tried putting the function in onPreExecute() also but the result is same.

I have also removed all the try-catch blocked to see if an error is showing but same.

I have changed the sequence of the AsyncTask but same.

I have checked my code multiple time for any typo or mismatch.

I have checked that all 4 functions are called inside the doInBackground() only.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • The code you shared doesn't share the right details to be certain, but I think you're seeing the effects of the fact that data is loaded from Firebase asynchronously. Any code that requires `total_item` must be inside the `onDataChange` or be called from there. See https://stackoverflow.com/questions/50434836/getcontactsfromfirebase-method-return-an-empty-list/50435519#50435519 – Frank van Puffelen Jul 16 '20 at 23:49
  • @FrankvanPuffelen `total_item` is a global variable whose value is updated inside `onDataChange` and since i am using `AsyncTask` to run the above function, it still load the data asynchronously ? and if so the what is the point of using `AsyncTask` ? – Devesh Kumar Sharma Jul 16 '20 at 23:59
  • 1
    Using an `AsyncTask` does not make asynchronous code run synchronously. The code you shared doesn't use an `AsyncTask`, nor is an `AsyncTask` needed when using Firebase (as the network/disk interaction runs on a separate thread already). But it really is as I said (and explained further in the link): any code that requires `total_item` must be inside the `onDataChange` or be called from there. – Frank van Puffelen Jul 17 '20 at 00:04
  • thank you so much. I have one last error, why is the output of `total_item` for `addListenerForSingleValueEvent` is showing as error in the log ? – Devesh Kumar Sharma Jul 19 '20 at 06:15
  • That's because you call `Log.e` in your code here: `Log.e(TAG, " Total items : " + total_item);`. `Log.e` logs as an error. – Frank van Puffelen Jul 19 '20 at 14:37

0 Answers0