1

I have a class MapActivity.java with method which search Cafes near to me and second method

private void parseLocationResult(JSONObject result) throws JSONException {

        String placeName = "", ivicinity = "";
        Double placeRating = 0.0;
        double latitude, longitude;

        JSONArray jsonArray = result.getJSONArray("results");

        if (result.getString(STATUS).equalsIgnoreCase(OK)) {

            mMap.clear();

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject place = jsonArray.getJSONObject(i);

                if (!place.isNull(NAME)) {
                    placeName = place.getString(NAME);
                }
                if (!place.isNull(VICINITY)) {
                    ivicinity = place.getString(VICINITY);
                }
                if(!place.isNull( RATING )){
                    placeRating = place.getDouble( RATING );
                }


                latitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
                        .getDouble(LATITUDE);
                longitude = place.getJSONObject(GEOMETRY).getJSONObject(LOCATION)
                        .getDouble(LONGITUDE);

                MarkerOptions markerOptions = new MarkerOptions();
                LatLng latLng = new LatLng(latitude, longitude);
                markerOptions.position(latLng);
                markerOptions.title(placeName + " : " + ivicinity);
                markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
                PlaceDetail cafe= new PlaceDetail(placeName,ivicinity,placeRating);

                Map<String, String> dataMap = new HashMap<>();
                dataMap.put("name", placeName);
                dataMap.put("address", ivicinity);
                dataMap.put("rating", String.valueOf(placeRating));
                db.collection("collection")
                        .add(dataMap)
                        .addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
                            @Override
                            public void onSuccess(DocumentReference documentReference) {
                            }
                        });
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 12f));
                mMap.addMarker(markerOptions);
            }

            Toast.makeText(getBaseContext(), jsonArray.length() + " Cafe(s) Found!",
                    Toast.LENGTH_LONG).show();
        } else if (result.getString(STATUS).equalsIgnoreCase(ZERO_RESULTS)) {
            Toast.makeText(getBaseContext(), "No Cafe Found in 4 Miles Radius!!!",
                    Toast.LENGTH_LONG).show();
        }

    }

which save my search results to Firestore. Saving to base works almost fine, but there is one problem. I would like to save only Cafes which are not already added. I think I need any check system - if there is already data with the same values as I found, don't save it, in other case save.

How to block the addition of data already stored there?

newbieHere
  • 276
  • 2
  • 15

1 Answers1

0

As I understand you would like to add data to your collections but you would like to ensure that these data will be unique in your database.

Natively, in Cloud Firestore you can't ensure uniqueness of data from either Rules or the SDK. Although I believe that the following links will help you to achieve this as they apply to similar use cases:

The accepted answer in this Stackoverflow Post explains how to achieve what you want with Rules.

Check also this article in which the approach is quite simple.

  1. Use a "Cafes" collection, in which every document will correspond a Cafe. The documentID of the document should be the name of the Cafe.

  2. When you try to save your search results to Firestore, check the "Cafes" collection to see if there is a documentId that fits the search results you would like to add every time. If the documentId doesn't exist then you add the search results to your collection.

Let me know if this was helpful.

tzovourn
  • 1,293
  • 8
  • 18