How to bypass the Firebase cache to refresh data (in Android app)?

后端 未结 7 1897
孤街浪徒
孤街浪徒 2020-11-30 21:48

On an Android application which must works offline most of the time I need, when it\'s online, to do some synchronous operations for i.e. :

User myUser =  My         


        
7条回答
  •  情歌与酒
    2020-11-30 22:33

    I tried both accepted solution and I tried transaction. Transaction solution is cleaner and nicer, but success OnComplete is called only when db is online, so you can't load from cache. But you can abort transaction, then onComplete will be called when offline (with cached data) aswell.

    I previously created function which worked only if database got connection lomng enough to do synch. I fixed issue by adding timeout. I will work on this and test if this works. Maybe in the future, when I get free time, I will create android lib and publish it, but by then it is the code in kotlin:

    /**
         * @param databaseReference reference to parent database node
         * @param callback callback with mutable list which returns list of objects and boolean if data is from cache
         * @param timeOutInMillis if not set it will wait all the time to get data online. If set - when timeout occurs it will send data from cache if exists
         */
        fun readChildrenOnlineElseLocal(databaseReference: DatabaseReference, callback: ((mutableList: MutableList<@kotlin.UnsafeVariance T>, isDataFromCache: Boolean) -> Unit), timeOutInMillis: Long? = null) {
    
            var countDownTimer: CountDownTimer? = null
    
            val transactionHandlerAbort = object : Transaction.Handler { //for cache load
                override fun onComplete(p0: DatabaseError?, p1: Boolean, data: DataSnapshot?) {
                    val listOfObjects = ArrayList()
                    data?.let {
                        data.children.forEach {
                            val child = it.getValue(aClass)
                            child?.let {
                                listOfObjects.add(child)
                            }
                        }
                    }
                    callback.invoke(listOfObjects, true)
                    removeListener()
                }
    
                override fun doTransaction(p0: MutableData?): Transaction.Result {
                    return Transaction.abort()
                }
            }
    
            val transactionHandlerSuccess = object : Transaction.Handler { //for online load
                override fun onComplete(p0: DatabaseError?, p1: Boolean, data: DataSnapshot?) {
                    countDownTimer?.cancel()
                    val listOfObjects = ArrayList()
                    data?.let {
                        data.children.forEach {
                            val child = it.getValue(aClass)
                            child?.let {
                                listOfObjects.add(child)
                            }
                        }
                    }
                    callback.invoke(listOfObjects, false)
                    removeListener()
                }
    
                override fun doTransaction(p0: MutableData?): Transaction.Result {
                    return Transaction.success(p0)
                }
            }
    

    If you want to make it faster for offline (to don't wait stupidly with timeout when obviously database is not connected) then check if database is connected before using function above:

    DatabaseReference connectedRef = FirebaseDatabase.getInstance().getReference(".info/connected");
    connectedRef.addValueEventListener(new ValueEventListener() {
      @Override
      public void onDataChange(DataSnapshot snapshot) {
        boolean connected = snapshot.getValue(Boolean.class);
        if (connected) {
          System.out.println("connected");
        } else {
          System.out.println("not connected");
        }
      }
    
      @Override
      public void onCancelled(DatabaseError error) {
        System.err.println("Listener was cancelled");
      }
    });
    

提交回复
热议问题