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.