1

I am writing an app that show restaurants and user reviews.

I want to display the reviews , I have the username, comment, date and user_photo.

enter image description here enter image description here

enter image description here

First question is should I save in review document the username or userId ?

Second question is how to display user`s photo? I can not store it in the document as URL.

Because if the user change the photo it will not change in review document. Note that user`s photo is stored in Firestore Storage.

What it is the best practice to solve this issue?

AdapterReview.java

public class AdapterReview extends FirestoreRecyclerAdapter<ReviewModel, HolderReviews>{

public AdapterReview(@NonNull FirestoreRecyclerOptions<ReviewModel> options) {
    super(options);
}
@Override
protected void onBindViewHolder(@NonNull HolderReviews holder, int i, @NonNull ReviewModel reviewObject) {
    holder.usernameTextView.setText(reviewObject.getUsername());
    holder.dateTextView.setText(reviewObject.getDate());
    holder.commentTextView.setText(reviewObject.getComment());
    holder.photoImageView.setImageURI(???);
}

@NonNull
@Override
public HolderReviews onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_adapter, parent, false);
    return new HolderReviews(view);
}
}

This method that create firestore recyclerview in MainActivity.java

  private void fireStoreRecyclerViews( ) {
    Query query = db.collection("reviews");

    FirestoreRecyclerOptions<ReviewModel> options = new FirestoreRecyclerOptions.Builder<ReviewModel>()
            .setQuery(query, ReviewModel.class)
            .build();

    adapterReview = new AdapterReview(options);
    RecyclerView recyclerView = view.findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this);
    recyclerView.setAdapter(adapterReview);

}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
andr111
  • 49
  • 4

1 Answers1

1

First question is should i save in review document the username or userId ?

Definitely the uid, since is unique. In case of user names, it could end up colliding and trying to write two documents with the same user name.

Second question is how to display user`s photo? i can not store it in the document as URL.

In most of cases we are using a library that can help us set an image to a view. For that I recommend you Glide. And as I see in your database, you are already storing the "photo" url in the document which is a correct approach.

Once you have added Glide dependencies, please change the following line of code:

holder.photoImageView.setImageURI(???);

to

Glide.with(context).load(reviewObject.getPhoto()).into(holder.photoImageView);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Sorry if my question is not clear, what i meant is I can not store user URL photo in review document. Because if I store URL in review document and the user updated it`s photo, it will be updated in user document and not in review document. So how to get user photo from user document and pass it to recyclerview? do I have to query user photo by user id ? and how – andr111 Sep 14 '19 at 13:25
  • 1
    What you say is called [denormalization](https://stackoverflow.com/questions/54258303/what-is-denormalization-in-firebase-cloud-firestore) and it is a quite common practice when it comes to Firebase. For a better understanding, I recommend you see this video, [Denormalization is normal with the Firebase Database](https://www.youtube.com/watch?v=vKqXSZLLnHA). It's for Firebase realtime database but same rules apply in case of Firestore. So what you said it's really normal in NoSQL world. – Alex Mamo Sep 15 '19 at 10:51