1

I'm new to Cloud Firestore, I wanted to implement favorite action in my Android app. I have a list of restaurants with each has a favorite action to it.

So, once the user clicks on the favorite button, I should save the name of the restaurant in the list in Firestore against that user.

And after making the restaurant favorite, the user can also hit the unfavorite button, then I should delete that entry from the list for that user.

I had done it using SQLite, can anyone help me doing it in Cloud Firestore?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Kiran Suvarna
  • 271
  • 6
  • 21

1 Answers1

1

To save a restaurant as a favorite, you need to create a Map and not an array beneath each user document. According to official documentation:

Although Cloud Firestore can store arrays, it does not support querying array members or updating single array elements.

So your database structure should look like this:

Firestore-root
    |
    --- users
         |
         --- uid
              |
              --- // user details
              |
              --- favoriteRestaurants
                        |
                        --- restaruantIdOne: true
                        |
                        --- restaruantIdTwo: true

In code, should look like this:

Map<String, Object> favoriteRestaurants = new HashMap<>();
Map<String, Object> restaurant = new HashMap<>();
restaurant.put("restaruantIdOne", true);
favoriteRestaurants.put("favoriteRestaurants", restaurant);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
DocumentReference uidRef = rootRef.collection("users").document(uid);
uidRef.update(favoriteRestaurants);

If you want to remove that a restaurant from favorites, please use the following code:

Map<String, Object> restaurant = new HashMap<>();
restaurant.put("favoriteRestaurants.restaruantIdOne", FieldValue.delete());
uidRef.update(restaurant);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I can able to save the favorite restaurant. But when I run this code for next restaurant it's replacing the old restaurant entry with the new one. – Kiran Suvarna May 15 '18 at 18:43
  • Be careful to have different restaurant ids and use `update` method, right? – Alex Mamo May 15 '18 at 18:48
  • Yeah, the Ids are unique. If I put all the restaurants in "restaurant" map and then update, it's saving all restaurants. But i need to save one restaurant at a time, because the user hits on favorite button on details page one at a time. – Kiran Suvarna May 15 '18 at 18:54
  • Please share the code where you are adding one restaurant at a time. – Alex Mamo May 15 '18 at 20:09
  • @AlexMamo: In my case am having an array of hash map inside the document. I want to add data to the existing. How to achieve this without overwriting? And am using custom id for the document, not the default one . – Mac_Play Aug 27 '19 at 14:38
  • @Mac_Play Please post another fresh question, so me and other Firebase developers can help you. – Alex Mamo Aug 27 '19 at 14:39
  • @AlexMamo https://stackoverflow.com/questions/57677213/how-to-add-data-to-the-array-of-hash-map-in-the-document-using-firestore – Mac_Play Aug 27 '19 at 14:45