Reputation: 53
The below code inserts data in the Firebase database and runs fine when my Android device has network connectivity. However, if I'm offline it does not sync directly with the online database. I would like to be notified if it's being cached rather than being synced directly.
DatabaseReference databaseIssue;
databaseIssue = FirebaseDatabase.getInstance().getReference("Issue");
String timeDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
databaseIssue.child(timeDate).setValue("test");
Is there anyway to implement somthing like simple try/catch around:
databaseIssue.child(timeDate).setValue("test");
A catch if the data is only cached?
From the Firebase documentation (https://firebase.google.com/docs/database/admin/save-data)
Adding a Completion Callback
DatabaseReference dataRef = ref.child("data");
dataRef.setValueAsync("I'm writing data", new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
if (databaseError != null) {
System.out.println("Data could not be saved " + databaseError.getMessage());
} else {
System.out.println("Data saved successfully.");
}
}
});
The set value does throw other exceptions: Firebase: Permission denied - setValue()
Does firebase throw an exception if it cannot connect to the online database and has to cache the data? Is there a way to log, if the data is being cached or synced?
Network Connectivity and Offline Writes
Firebase Node.js and Java clients maintain their own internal version of >any active data. When data is written, it is written to this local version first. The client then synchronizes that data with the database and with other clients on a 'best-effort' basis.
As a result, all writes to the database will trigger local events immediately, before any data has even been written to the database. This means that when you write an application using Firebase, your app will remain responsive regardless of network latency or Internet connectivity.
Once connectivity is reestablished, we'll receive the appropriate set of events so that the client "catches up" with the current server state, without having to write any custom code.
Upvotes: 0
Views: 1557
Reputation:
check your permission for firebase console in your project like below code .. { "rules": { ".read": true, ".write": true } }
and i hope you add internet permission into android manifest file ..
<uses-permission android:name="android.permission.INTERNET"/>
all things are check you insert data right way. if any problem you can refer this link .. https://www.simplifiedcoding.net/firebase-realtime-database-crud/
Upvotes: 0
Reputation: 599746
Writing to the database while you're not connected to the Firebase server is not considered an error condition. So no error is thrown in such cases.
The best you can do is detect whether you're connected to the Firebase servers separately, by attaching a listener to .info/connected
as shown in the documentation on detecting connection state. When .info/connected
is false
your app is not connected to the network, so local writes will be queued.
Upvotes: 0
Reputation: 1219
I hope this will work for you
Below line put in your application class onCreate()
method
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
it will cached data, when internet connection comes it will sync data to firebase database
Upvotes: 0