0

I am receiving data from Firebase and when it receives all data, I want to save the data to Room with AsyncTask. However, I don't know when the data extraction will end because it is untimely. That's why it returns 0 every time. I could not find a clear article on this subject. Could you please clarify the issue?

Note: I run this method inside a service

  public void userRecipeDataRequest() {
       userItemsList.clear();
       DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("path").child("child");
       rootRef.addValueEventListener(new ValueEventListener() {
           @Override
           public void onDataChange(@NonNull DataSnapshot snapshot) {
               for (DataSnapshot data : snapshot.getChildren()) {
                   UploadRecipeModel recipeModel = data.getValue(UploadRecipeModel.class);
                   userItemsList.add(recipeModel);
               }

               Log.d("TAG_DATA_SIZE ", " " + userItemsList.size());
           }



           @Override
           public void onCancelled(@NonNull DatabaseError error) {

           }
       });

       Log.d("TAG_DATA_SIZE", " " + userItemsList.size());

      new InsertUserData().execute(userItemsList);
   }

EDIT

When I put the asynctask operation inside the for loop, sometimes 0 and sometimes all the data comes in.. but still the problem is not solved

  • 1
    `onDataChange()`, `onComplete()` and `onSuccess()` are called when the data is received – Muhammad Junaid Khalid Feb 02 '22 at 14:30
  • @MuhammadJunaidKhalid How will I know when the transaction is over? –  Feb 02 '22 at 14:31
  • These functions are called after the transaction is over – Muhammad Junaid Khalid Feb 02 '22 at 14:32
  • I always get 0 when I check with Logcat –  Feb 02 '22 at 14:34
  • But after a certain time when I use it in an activity, the data comes –  Feb 02 '22 at 14:36
  • 1
    Data is loaded from Firebase (and most modern cloud APIs) asynchronous, since it may take some time to get. While the data is being loaded, your main code continues to run and your `new InsertUserData().execute(userItemsList)` runs *before* any data has been loaded. For this reason, any code that needs the data from the database must be *inside* the `onDataChange` that is called when the data is available, or be called from there. I linked some examples. – Frank van Puffelen Feb 02 '22 at 14:43
  • Also: you should never leave `onCancelled` empty as you're ignoring potential error. At its minimum it should be: `public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }` – Frank van Puffelen Feb 02 '22 at 14:46
  • @FrankvanPuffelen How do I know when my job is finished? I need to run AsyncTask after the process is finished –  Feb 02 '22 at 14:51
  • 1
    @test721 run that task in onSuccess, onComplete or onDataChange – Muhammad Junaid Khalid Feb 02 '22 at 14:53
  • nothing changes –  Feb 02 '22 at 14:55
  • @test721 "nothing changes" is just not very likely given the feedback we've given so far. If you tried to apply the fixes I linked and that was given by others in comments and you still can't get it to work, edit your question and add the minimal updated code at the bottom, ideally also with the output your log statements generate now. – Frank van Puffelen Feb 02 '22 at 23:27
  • There is no way you can do that. Firebase API is asynchronous. So please check the duplicates to see how can you solve this using a callback. You might also be interested in reading this article, [How to read data from Firebase Realtime Database using get()?](https://medium.com/firebase-tips-tricks/how-to-read-data-from-firebase-realtime-database-using-get-269ef3e179c5). – Alex Mamo Feb 04 '22 at 07:00
  • @AlexMamo Need a simple and real solution? why didn't they think of that –  Feb 09 '22 at 23:14
  • You will need to nest the loading of data, and only call `new InsertUserData().execute(userItemsList)` after all `userTariflerItemsList.add(recipeModel);` calls are done. If you've done that and still have the problem, edit your question to show how you applied the feedback from the comments and from the links I provided to your code. That will also make it eligible to be reopened. – Frank van Puffelen Feb 10 '22 at 00:33
  • @FrankvanPuffelen this doesn't look healthy at all, sometimes it comes 0, sometimes it comes all... Firebase needs to come up with a solution for this, for example, it needs to fetch the data when the button is pressed.. both synchronous and asynchronous options should be available. –  Feb 10 '22 at 01:03
  • I don't know how to enable the question again.. the answers given are not a solution, I don't understand why the question is closed –  Feb 10 '22 at 01:03
  • It is closed because you're struggling with asynchronous loading, in the same way as the questions I linked. If you've addressed your problem in the way that is mentioned there (so with nesting the calls, and custom callback) you can `edit` your question (just like you did when you changed the title an hour ago) to show how you modified the code. – Frank van Puffelen Feb 10 '22 at 01:55
  • there was a box, I ticked it, I hope the question will open up again –  Feb 10 '22 at 08:27

0 Answers0