9

I am retrieving data from firebase database inside for loop , but problem is that firebase functions are not execute synchronously , i know firebase functions are asynchronous . is there any solution for that ?

Code :-

registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                peopleModelList.clear();

                for(DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    peopleModel = new PeopleModel();

                    peopleHashMap =(HashMap)snapshot.getValue();

                    peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
                    peopleModel.setNickname((String)peopleHashMap.get("nickname"));
                    peopleModel.setChatRoom(snapshot.getKey());

                    Log.d("kkkk","1st");

                    if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
                    {
                        registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                peopleModel.setRefresh_token((String)dataSnapshot.child("refresh_token").getValue());
                                peopleModelList.add(peopleModel);
                                Log.d("kkkk","2nd");
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
                    }
                    Log.d("kkkk","3rd");

                }
                iCallBackPeople.peopleAct(peopleModelList);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });

Output :-

1st
3rd
2nd

But i want output like this

1st
2nd
3rd

Note :- i searched , but never got any solution !

How to get data from Firebase Database in a loop?

Firebase addListenerForSingleValueEvent excute later in loop

I tried this but not worked for me, it is fill my list with last item !

public void listOfUsers(final ICallBackPeople iCallBackPeople) {
        count=0;
        //peopleModelList = new ArrayList<>();
        sortedMap = new TreeMap<>();
       // peopleModelList = new ArrayList<PeopleModel>(sortedMap.values());
        registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
        {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
             //   peopleModelList.clear();

                for(DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    peopleModel = new PeopleModel();

                    peopleHashMap =(HashMap)snapshot.getValue();

                    peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
                    peopleModel.setNickname((String)peopleHashMap.get("nickname"));
                    peopleModel.setChatRoom(snapshot.getKey());

                    //Object chatRoom = snapshot.getKey();
                    Log.d("kkkk","1st");

                    if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
                    {

                        addItem(count,iCallBackPeople);
                        count++;
                    }
                    Log.d("kkkk","3rd");

                }
                Log.d("kkkk","end");
                //iCallBackPeople.peopleAct(peopleModelList);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

    private void addItem(final int index, final ICallBackPeople iCallBackPeople) {
        registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                peopleModel.setRefresh_token((String)dataSnapshot.child("refresh_token").getValue());
                Log.d("kkkk","2nd");
                sortedMap.put(index, peopleModel);
                // sortedMap will sort your list by key (in this case, key is integer)


                 if(sortedMap.size()==2)
                 {
                     peopleModelList = new ArrayList<PeopleModel>(sortedMap.values());
                     iCallBackPeople.peopleAct(peopleModelList);
                 }

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

I tried this but not getting all of the item only getting list with last item in snapshot

public void listOfUsers(final ICallBackPeople iCallBackPeople) {

        peopleModelList = new ArrayList<>();

        registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
        {
            PeopleModel peopleModel;
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                peopleModelList.clear();

                for(final DataSnapshot snapshot : dataSnapshot.getChildren())
                {
                    peopleModel = new PeopleModel();

                    peopleHashMap =(HashMap)snapshot.getValue();

                    peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
                    peopleModel.setNickname((String)peopleHashMap.get("nickname"));
                    peopleModel.setChatRoom(snapshot.getKey());


                    if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
                    {
                        getToken(new CallBackGetToken() {
                            @Override
                            public void token(String token) {
                                peopleModel.setRefresh_token(token);
                                Toast.makeText(context, token, Toast.LENGTH_SHORT).show();
                                peopleModelList.add(peopleModel);

                                //here i am checking my peopleModelList size is equal or not to snapshot
                                if(peopleModelList.size()==2)
                                    iCallBackPeople.peopleAct(peopleModelList);
                            }
                        },peopleModel);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
            }
        });
    }

    private void getToken(final CallBackGetToken callBackGetToken ,PeopleModel peopleModel) {
        registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(DataSnapshot dataSnapshot) {
                                callBackGetToken.token((String)dataSnapshot.child("refresh_token").getValue());
                            }

                            @Override
                            public void onCancelled(DatabaseError databaseError) {

                            }
                        });
    }
iamkdblue
  • 3,448
  • 2
  • 25
  • 43
  • This doesn't mean that it doesn't work. It works but it only shows the name of the last object. And for that, you should get that statement out of the for loop because for each iteration that statement is called, ending up in having only one item, the last one, right? – Alex Mamo May 02 '18 at 16:21
  • yea , ex- suppose i having model in which having 3 members , so i am getting 2 data members value before calling callback function and 1 data members value getting after callback function and then put into list! – iamkdblue May 02 '18 at 16:35
  • Please resond with `@`. Also in this case you need to wait for the data. – Alex Mamo May 04 '18 at 17:29

3 Answers3

2

I think you should pass through an interface(create one and implement at this class). the method inside the interface should have a string as a parameter (it will receive your peopleModel.getChatWith() ) .

Inside your if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))you should request your interface.

I think is a solution for your asynchronously question.

4ury0n
  • 105
  • 1
  • 2
  • 13
  • well, i would try to use EventBus, Multithread methods (with RX Android), or even BroadCast Receivers, one of those three certainly should solve your problem. – 4ury0n Apr 26 '18 at 05:34
  • Thank You ! can you provide some link from where i can learn or try something ! – iamkdblue Apr 26 '18 at 05:41
  • 1
    https://medium.com/@kevalpatel2106/code-your-next-android-app-using-rxjava-d1db30ac9fcc – 4ury0n Apr 26 '18 at 06:37
1

Here is my solution:

int count =0;

SortedMap<Integer,PeopleModel> sortedMap;
private void set() {
     count =0;
    registrationReference.child(userId).child("generated_links").addListenerForSingleValueEvent(new ValueEventListener()
    {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            peopleModelList.clear();
            sortedMap = new TreeMap<>();
            for(DataSnapshot snapshot : dataSnapshot.getChildren())
            {
                peopleModel = new PeopleModel();

                peopleHashMap =(HashMap)snapshot.getValue();

                peopleModel.setChatWith((String)peopleHashMap.get("chatWith"));
                peopleModel.setNickname((String)peopleHashMap.get("nickname"));
                peopleModel.setChatRoom(snapshot.getKey());

                Log.d("kkkk","1st");

                if(peopleModel.getChatWith()!=null && !("".equals(peopleModel.getChatWith())))
                {
                    addItem(count);
                    count++;
                }
                Log.d("kkkk","3rd");

            }
            iCallBackPeople.peopleAct(peopleModelList);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

private void addItem(final int index) {
    registrationReference.child(peopleModel.getChatWith()).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            peopleModel.setRefresh_token((String)dataSnapshot.child("refresh_token").getValue());
            Log.d("kkkk","2nd");
            sortedMap.put(index, peopleModel);
            // sortedMap will sort your list by key (in this case, key is integer)
            // you can get peopleModelList = new ArrayList<>(sortedMap.values);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}
Nha Phạm Thị
  • 409
  • 4
  • 10
1

Maybe I'm over simplifying it, but I feel like you should just use simple recursion.

List<People> peopleList;
int currentIndex = 0

private void myFirebaseMethodAction(person){
     registerListener.onCallbacks(new ValueListener(){
             onDataChanged(){
                  person.addStufforDoStuff();
                  if(peopleList.size > currentIndex + 1){
                      myFirebaseMethodAction(peopleList.get(++currentIndex)
                  }
             }
             onFailed(){
                  //handle it
             }
     }
}

Does that make sense? Just saying walk the list with an index, simple. btw above is pseduo code, don't copy verbatim lol.

Sam
  • 5,342
  • 1
  • 23
  • 39
  • thanks for your support , actually till now i am not able to solve that problem , by mistakenly i put some wrong code , in my actual code , so result was getting expected but now when i see that wrong code there then i removed wrong code , now i again encounter that problem my list is not getting update . – iamkdblue May 02 '18 at 11:23
  • I'm not sure what you are asking now? Your question was how to synchronize your list walk to wait on callbacks. I provided that solution. Are you asking something different now? What list is empty? That wasn't part of your question, so I'm not sure how to respond. – Sam May 02 '18 at 19:12