android - Firebase Offline Capabilities and addListenerForSingleValueEvent -


whenever use addlistenerforsinglevalueevent setpersistenceenabled(true), manage local offline copy of datasnapshot , not updated datasnapshot server.

however, if use addvalueeventlistener setpersistenceenabled(true), can latest copy of datasnapshot server.

is normal addlistenerforsinglevalueevent searches datasnapshot locally (offline) , removes listener after retrieving datasnapshot once (either offline or online)?

how persistence works

the firebase client keeps copy of data you're actively listening in memory. once last listener disconnects, data flushed memory.

if enable disk persistence in firebase android application with:

firebase.getdefaultconfig().setpersistenceenabled(true);  

the firebase client keep local copy (on disk) of data app has listened to.

what happens when attach listener

say have following valueeventlistener:

valueeventlistener listener = new valueeventlistener() {     @override     public void ondatachange(datasnapshot snapshot) {         system.out.println(snapshot.getvalue());     }      @override     public void oncancelled(firebaseerror firebaseerror) {         // no-op     } }; 

when add valueeventlistener location:

ref.addvalueeventlistener(listener);  // or ref.addlistenerforsinglevalueevent(listener);  

if value of location in local disk cache, firebase client invoke ondatachange() value local cache. if initiate check server, ask updates value. may subsequently invoke ondatachange() again if there has been change of data on server since last added cache.

what happens when use addlistenerforsinglevalueevent

when add single value event listener same location:

ref.addlistenerforsinglevalueevent(listener); 

the firebase client (like in previous situation) invoke ondatachange() value local disk cache. not invoke ondatachange() more times, if value on server turns out different. note updated data still requested , returned on subsequent requests.

this covered in how firebase sync work, shared data?

solution , workaround

the best solution use addvalueeventlistener(), instead of single-value event listener. regular value listener both immediate local event , potential update server.

as workaround can call keepsynced(true) on locations use single-value event listener. ensures data updated whenever changes, drastically improves chance single-value event listener see current value.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -