Java 如果Firestore中还没有数据,如何将数据添加到Firestore中
我有一个类Java 如果Firestore中还没有数据,如何将数据添加到Firestore中,java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我有一个类MapActivity.java,其中包含搜索我附近咖啡馆的方法和第二个方法 private void parseLocationResult(JSONObject result) throws JSONException { String placeName = "", ivicinity = ""; Double placeRating = 0.0; double latitude, longitude; JSON
MapActivity.java
,其中包含搜索我附近咖啡馆的方法和第二个方法
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();
}
}
private void parseLocationResult(JSONObject结果)抛出JSONException{
字符串placeName=“”,ivicinity=“”;
双重评级=0.0;
双纬度,经度;
JSONArray JSONArray=result.getJSONArray(“结果”);
if(result.getString(STATUS.equalsIgnoreCase(OK)){
mMap.clear();
for(int i=0;i
将我的搜索结果保存到Firestore。“保存到基础”几乎可以正常工作,但有一个问题。我只想保存尚未添加的咖啡馆。我想我需要任何检查系统-如果已经有与我发现的值相同的数据,不要保存,否则保存
如何阻止添加已存储在其中的数据?据我所知,您希望将数据添加到集合中,但希望确保这些数据在数据库中是唯一的 从本质上讲,在云Firestore中,您无法确保来自规则或SDK的数据的唯一性。尽管我相信以下链接将帮助您实现这一点,因为它们适用于类似的用例: 解释如何使用规则实现您的目标 还可以检查其中的方法非常简单
让我知道这是否有用 据我所知,您希望向集合中添加数据,但希望确保这些数据在数据库中是唯一的 从本质上讲,在云Firestore中,您无法确保来自规则或SDK的数据的唯一性。尽管我相信以下链接将帮助您实现这一点,因为它们适用于类似的用例: 解释如何使用规则实现您的目标 还可以检查其中的方法非常简单
让我知道这是否有用 您将使用什么标准来确定某些数据是否已经存在?通过“某些数据已经存在”是什么意思?请用@回复。您将使用什么标准来确定某些数据是否已经存在?通过“某些数据已经存在”是什么意思?请同时回复@。