问题描述:

I understood that Firebase has the capability to store the data locally during offline, and perform write when the device is back online.

However I would like to disable this feature for some writes in my application, as I would like to prevent the users to spam the database. Thanks.

网友答案:

All writes are queued until there is a connection to the Firebase servers again. There is no API to control which writes are queued and which are not.

The only loophole to this is that transactions are currently not persisted to disk and thus will be lost when the application is restarted.

网友答案:

According Frank, there is no way we can control the write queue. Thus, I decided to write a utility to check for network connection. The write will be only executed if there is a connection.

NetworkUtil.java:

public class NetworkUtil {

    private Context context;

    public NetworkUtil(Context context) {
        this.context = context;
    }

    public boolean hasNetwork() {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final NetworkInfo info = mConnectivityManager.getActiveNetworkInfo();
        if (info == null || !info.isConnected())
            return false;
        else
            return true;
    }

}

And I added this lines to my activity:

/* Check for network */
NetworkUtil networkUtil = new NetworkUtil(getApplicationContext());
boolean hasNetwork = networkUtil.hasNetwork();

if (hasNetwork)
    /* perform write to firebase */
相关阅读:
Top