android - Firebase looping, How can I stop this from looping -


i have been trying set value change listener this:

final databasereference chat_ref =  firebasedatabase.getinstance().getreference(constants.arg_chat_rooms +    "/" + room_type_1);   chat_ref.addvalueeventlistener(new valueeventlistener() {       @override       public void ondatachange(final datasnapshot datasnapshot) {           showalert(datasnapshot);       }        @override       public void oncancelled(databaseerror databaseerror) {        }  }); 

the problem that, parenthesis after showalert method keeps looping over, creating several alerts when want one.

what causing loop , how can address it? appreciated!

i not sure getting wrong, because loops well:

chat_ref.addlistenerforsinglevalueevent(new valueeventlistener() {     @override     public void ondatachange(datasnapshot datasnapshot) {         showalert(datasnapshot);     }      @override     public void oncancelled(databaseerror databaseerror) {      } }); 

you have several alerts because of way in valueeventlistener works. remember valueeventlistener not loop interface. please see more details here. , see in offical doc, triggered every time data @ particular location changes.

to solve this, call of showalert() out ondatachange method.

hope helps.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -