The idea is as follows: there is a node locations / ns / we and there is a node users / location / which contains the keys ns and we. When you click on a button, the coordinates on 1 and 2 nodes change, that is, it was:

  • users / uid / location / ns: 10
  • users / uid / location / we: 1
  • locations / 10/1

It became:

  • users / uid / location / ns: 11
  • users / uid / location / we: 1
  • locations / 11/1

Base example

I need to get all the nodes that are stored in the current coordinate, that is, locations / currentNS / currentWE.

I put the listener on the values ​​of users / UID / location / ns users / UID / location / we, get the current coordinates and in the same listener put the listener on the node locations / currentNS / currentWE. The problem is that if you run a step back and forth, then the onChildAdded, onChildRemoved, etc. methods are run several times. If I go to 10 1 5 times, then at step 5, the methods will run 5 times each, although this link only has 1 node. Such impression that listeners accumulate on number of steps. What am I doing wrong?

It works when moving north

public void onNorth(View view) { Map<String, Object> update = new HashMap<>(); update.put("locations/"+ns+"/"+we+"/"+userId, null); ++ns; update.put("users/" + userId + "/location/ns", ns); update.put("locations/"+ns+"/"+we+"/"+userId, new User("Bot")); mFirebaseDatabase.getReference().updateChildren(update); } 

The listener by value gets the current coordinates and starts the listener setListenerForCurrentLocation

 mFirebaseDatabase = FirebaseDatabase.getInstance(); mReferenceLocation = mFirebaseDatabase.getReference("users/" + userId + "/location"); if (mValueEventListener == null) { mValueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { Location location = dataSnapshot.getValue(Location.class); ns = location.getNs(); we = location.getWe(); //Listener on current position(locations) setListenerForCurrentLocation(ns,we); } @Override public void onCancelled(DatabaseError databaseError) { } }; mReferenceLocation.addValueEventListener(mValueEventListener);} mReferenceLocations = mFirebaseDatabase.getReference("locations/" + ns+"/"+we); 

The setListenerForCurrentLocation method starts the listener at the current coordinate:

 public void setListenerForCurrentLocation(long ns, long we){ mChildEventListenerLocations = new ChildEventListener() { @Override public void onChildAdded(DataSnapshot dataSnapshot, String s) { Log.e("onChildAdded", dataSnapshot.getKey()); } @Override public void onChildChanged(DataSnapshot dataSnapshot, String s){ Log.e("onChildChanged", dataSnapshot.getKey()); } @Override public void onChildRemoved(DataSnapshot dataSnapshot) { Log.e("onChildRemoved", dataSnapshot.getKey()); } @Override public void onChildMoved(DataSnapshot dataSnapshot, String s) { Log.e("onChildMoved", dataSnapshot.getKey()); } @Override public void onCancelled(DatabaseError databaseError) { } }; mReferenceLocations.addChildEventListener(mChildEventListenerLocations); } 
  • Well, because hanging listeners in a callback and on onDataChange is an epicfile, at best, your ip will simply go to a timeout, or just guard will add you to the blacklist - Shwarz Andrei
  • I understand this, but due to inexperience, I don’t know how to ... can you tell me how you would have done everything to work as it should? - Albert

0