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