0

In Android studio, I want function getUsers to execute before getPosts, but the execution order is wrong,
and in my firebase real time database, I have 12 users,only two "fff" printed in logcat, how to solve the problem??

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        text = view.findViewById(R.id.test);
        fuser = FirebaseAuth.getInstance().getCurrentUser();
        getUsers();
        getPosts();
         

        return view;
    }

getUsers()

private void getUsers() {
 FirebaseDatabase.getInstance().getReference().child("users").addValueEventListener(newValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                Log.e("si", "fff" + "");

                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    User user = snapshot.getValue(User.class);
                    Log.e("si", "fff" + "");

                    users.add(user.getId());
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

getPosts()

 private void getPosts() {
        FirebaseDatabase.getInstance().getReference().child("Posts").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Log.e("p", "pp" + "");
                    Post post = snapshot.getValue(Post.class);
posts.add(post.getPostid());
                }
                dataSet();

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

            }
        });
    }
            

the logcat:

1607174523.915 21527-21527/com.example.signup E/p: pp
1607174523.915 21527-21527/com.example.signup E/p: pp
1607174523.915 21527-21527/com.example.signup W/ClassMapper: No setter/field for isslod found on class com.example.signup.Model.Post
1607174523.916 21527-21527/com.example.signup E/p: pp
1607174524.212 21527-21527/com.example.signup E/si: fff
1607174524.219 21527-21527/com.example.signup I/chatty: uid=10159(com.example.signup) identical 11 lines
1607174524.219 21527-21527/com.example.signup E/si: fff
1607174525.183 21527-21610/com.example.signup V/FA: Inactivity, disconnecting from the service

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
roro roor
  • 137
  • 1
  • 4
  • 8
  • https://stackoverflow.com/questions/57330766/why-does-my-function-that-calls-an-api-return-an-empty-or-null-value – a_local_nobody Dec 05 '20 at 13:54
  • Data is loaded from Firebase asynchronously, and while that is going on, your main code continues. Then once the data is loaded, your `onDataChange` is called. For this reason, any code that depends on data from the database needs to be inside `onDataChange` or be called from there. I added a link to a longer example of how to do this. – Frank van Puffelen Dec 05 '20 at 15:44

0 Answers0