0

I am using both the Realtime Database and Firestore in my social media application, am currently only using the Realtime Database for storing the user details and for messaging while in the Firestore I have a status videos collection that has details of the status video and a field that stores the id of the user who posted it, As status videos are being fetched ...the id is obtained and put into an array so that I can use each id to fetch the user details from the Realtime Database, The challenge is that every time I try to fetch details of the user it only makes one call in the onBindView method and one view only receives the data fetched from the realtime database in terms of the name and profile picture...is there a way alternatively to make calls in line with the number of datasets I have to ensure that data is fetched for all Ids in the array?

here is the code from my main activity:

  private void getStatusVid() {


    database.collection(Constants.KEY_STATUS)
            .limit(25)
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull @NotNull Task<QuerySnapshot> task) {
            if(task.isSuccessful()&& task.getResult()!=null){
               for (QueryDocumentSnapshot queryDocumentSnapshot:task.getResult()){

                   mVidsList.add(new StatusVids(queryDocumentSnapshot.getString(Constants.KEY_USER_ID),queryDocumentSnapshot.getString(Constants.KEY_COVER),queryDocumentSnapshot.getString(Constants.KEY_NAME),queryDocumentSnapshot.getString(Constants.KEY_TITLE),""));
                   

               }


                if(mVidsList.size()>0){

                    mRecyclerVids.setAdapter(new StatusVidsAdapter(mVidsList,getActivity()));
                }


            }

        }
    });
}

and here is the code being exexuted in onBindView:

 mRef.child(Constants.KEY_COLLECTION_USERS).child(mList.get(position).userId).addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {

          for(DataSnapshot snappy: snapshot.getChildren()){

          }

              if(snapshot.exists()){

                     final String pictureUrl=snapshot.child("profile").getValue(String.class);

                      Glide.with(context)
                              .load(pictureUrl)
                              .circleCrop()
                              .into(holder.profile);


              }


      }

      @Override
      public void onCancelled(@NonNull @NotNull DatabaseError error) {
          Toast.makeText(context,error.getMessage(),Toast.LENGTH_SHORT).show();
      }
  });
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
mike
  • 15
  • 1
  • 5
  • What exactly is wrong with shared code? Do you have any errors? – Alex Mamo Sep 26 '22 at 15:14
  • The main reason I shared the code was for any modification suggestions to complete the expected task, inshort am avoiding to continuously put the users profile image and name in every status video document because they are times users update images and the images in the status videos would be outdated,that's why want to clarify if they's anyway I can retrieve the users details in another collection or node while fetching the data in the status video collection and the combine the details in the User interface – mike Sep 26 '22 at 15:33

1 Answers1

0

in short am avoiding continuously putting the user's profile image and name in every status video document because they are times users update images and the images in the status videos would be outdated.

In that case, you should update the name and the image inside all videos. It's true that might be some kind of costly since you need to update those values in each place it exists, but that's a requirement when you hold them there.

Another solution would be to only hold a reference that points to the user profile, a case in which you'll need to create another database call, in order to display the user data.

So it's always a trade, so you need to calculate which one of the solutions works best for you.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • 1
    Thank you sir,I might say most of your solutions are really helpful,for the second tradeoff for reference ....is there a way to make calls concurrently using async task? – mike Sep 26 '22 at 16:31
  • [The AsyncTask API is deprecated](https://stackoverflow.com/questions/58767733/the-asynctask-api-is-deprecated-in-android-11-what-are-the-alternatives). So I recommend you use modern technologies like [LiveData](https://developer.android.com/topic/libraries/architecture/livedata). Or if you want to improve your skills in Android development, learn Kotlin ;) – Alex Mamo Sep 26 '22 at 17:34
  • 1
    Thanks allot for your recommendations, Mr Alex – mike Sep 27 '22 at 17:27