i have a problem with my code. I want only to listen when the document is added or deleted. The code is working nearly good. Now when the doc is added - toast appear. That works fine. But when i look at the read counter in my database - it increasing so quickly, like the function is 100% time reading all the data from database - not only for changes, like i want. I need to listen only for the changes in collection. Is it a way to do this in android? I have a webapp and there my javascript code is working fine, read data only when the change appear.
Below my code:
public void listenToDocument() {
// [START listen_document]
myDatabase.collection("markery").addSnapshotListener(new EventListener<QuerySnapshot>()
{
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w(TAG, "listen:error", e);
return;
}
for (DocumentChange documentChange : snapshots.getDocumentChanges()) {
switch (documentChange.getType()) {
case ADDED:
String popup_data = documentChange.getDocument().getData().get("popup_data").toString();
GeoPoint geop = (GeoPoint) documentChange.getDocument().getData().get("geop");
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, popup_data, Toast.LENGTH_LONG);
toast.show();
double lng = geop.getLongitude();
double lat = geop.getLatitude();
symbolLayerIconFeatureList.add(Feature.fromGeometry(
Point.fromLngLat(lng, lat)));
break;
case MODIFIED:
break;
case REMOVED:
break;
}
}
}
});
// [END listen_document]
}