In my Android app I have to keep an existing SQLite database, but in order to sync elements (tasks from a todo-list) to multiple devices I decided to use Firebase Database.
So I'm not enabling Firebase Database offline mode, since I have to use SQLite for that (it's for a college project).
So what I'm doing for writes is that, whenever some data gets added or removed from the local SQLite database that change is also made to the Firebase one.
All this works as expected if the user is connected to the Internet, or even if the user adds a task offline and then becomes online while being on the app.
But if the user adds a task offline, then closes the app and then becomes online in another app, the change doesn't get pushed to the database, even if the user opens my app again.
Any ideas?
Thank you.
As you did not enable the Firebase Database offline mode all data your app trying to push are not saved in persistent storage, only in memory. Thats why all these data are not pushed to Firebase after application restart.
So you need some synchronization algorithm at application startup which pushes data from you local database to firebase (and vise versa).
For example, you can use timestamp marker (System.currentTimeMillis()) in both local and firebase side which always should be updated on any database change.
Then at application startup you can just check this marker:
if local marker is greater than the remote one - that means that
remote database is outdated. So just copy all your local sql database
data to firebase.
if remote marker is greater than the local one - just clean local db
and download all data from firebase.
if both markers are equal - that means that everything is up to date,
do nothing.
Hope this helps :)
Related
It is about Firebase's local cached data in Android.
If the Android user, has 1-2 years data in Firebase,
is there a way to enable, setup, or filter Firebase persistence in local,
so that it only create local cache for the last month ?
So we don't the whole data in local cache, but only the last month.
The Firebase local cache is not designed to be manually populated. It's managed automatically, and you don't have much control over what gets stored in it. It will store data from recent queries, and it will evict data that hasn't been used. You can't choose which data will or will not be cached - it work with everything or nothing at all.
The local cache is meant to be helpful when the user temporarily loses their network connection. It's not meant for full offline support.
If you need a cache for very specific data that you can control, you will need to build that yourself.
After making FirebaseDatabase.getInstance().setPersistenceEnabled(true); When query to get data
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("notifications")
.child("entities")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid().toString())
.orderByChild("seen")
.equalTo(0);
From data tree
it gives old data that is in cache instead of new changed data in Firebase databse.But When I change setPersistenceEnabled(true) to setPersistenceEnabled(false) then everything is fine While I also want firebase data to be persisted in cache and also change When change is made to Firebase database.Please help to maintain Firebase data persistence along with realtime change.
If you are using:
setPersistenceEnabled(true);
It means that you'll be able to query your the database even if you are offline. This is happening because Firebase creates a local copy of your database that will persist after your app/device restarts. Every change that is made while you are offline, will be updated on Firebase servers once you are back online. To be more clear, every client that is using a Firebase database and uses setPersistenceEnabled(true) maintains it's own internal (local) version of the database. When data is updated, it is first written to this local version of the database.
So, by enabling persistence, any data that the Firebase Realtime database client would sync while online, persists to disk and is available offline, even when the user or operating system restarts the app. This means that your app will work as it would be online by using the local data stored in the cache.
But, there is no way to stop the retrieval of the data from the cache while you are not connected to the server, as you cannot force the retrieval of the data from the cache while you're connected to the server and unfortunately this behaviour cannot be changed.
I have an android application which uses SQLite database (stored in APK) for storing offline working data. The app is used only in offline mode.
Now I want to create iOS version and I want to implement idea of synced databases on both platforms - Change database on server and it will update databases in device when will be online
My first idea was to replace SQLite to Firebase Realtime database but there is a problem with offline mode. Is there a way to cache the whole JSON for offline use?
Or is it easier to use a cloud for storing SQLite database and implement syncing with new versions of the database?
Thank you for your responses.
The Firebase Realtime Database client automatically downloads the data at these locations and keeps it in sync even if the reference has no active listeners. You can turn synchronization back off with the following line of code.
DatabaseReference scoresRef = FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
scoreRef.setPersistedEnabled();
But this need very first internet connection to sync, this will no enable data without that first request.
For detail about the offline capabilities, head out to the official doc link here.
Is it possible that an application's listview updates the data from Firebase without even opening the application?
I am working on a restaurant application which has to work offline, so the user must NOT open the application to get the new menu, rather it must be updated automatically (whenever the user is connected to internet) even if the application isn't opened. Is it possible to be achieved?
I think you're confusing updating the DATA with updating the MENU. A menu is a rendered object - if you're driving it with a listview, naturally it won't be updated when the app isn't running because there's no reason to draw a listview update if the listview isn't being shown.
But you can absolutely update the data, and we do this in News Rush to give the user access to new data while offline, if they were online earlier. Just make sure persistence is enabled and run the same query that drives your listview. Make sure the path/params are identical. Persistence will remember that data and if the user is offline the new menu will still show correctly the next time the app is started.
You should update it outstide the app, but, you can enable offline capabilities,
Enabling Offline Capabilities on Android
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Firebase apps work great offline and we have several features to make
the experience even better. Enabling disk persistence allows your app
to keep all of its state even after an app restart. We provide several
tools for monitoring presence and connectivity state.
To keep data fresh:
Keeping Data Fresh
The Firebase Realtime Database synchronizes and stores a local copy of
the data for active listeners. In addition, you can keep specific
locations in sync.
DatabaseReference scoresRef =
FirebaseDatabase.getInstance().getReference("scores");
scoresRef.keepSynced(true);
The client will automatically download the
data at these locations and keep it in sync even if the reference has
no active listeners. You can turn synchronization back off with the
following line of code.
scoresRef.keepSynced(false); By default, 10MB of previously synced
data will be cached. This should be enough for most applications. If
the cache outgrows its configured size, the Firebase Realtime Database
will purge data that has been used least recently. Data that is kept
in sync, will not be purged from the cache.
Read more:
Enabling Offline Capabilities on Android
I'm planning to create an android app for customers(shop) that have an offline database so the users can use it offline. After that I will create a Website for the owner of the shop so he/she can post new items on the website then save it to the online database. My question is how can I constantly update the offline database of the android app whenever there's is a new item on the online database? Any suggestion or theory that I can use will help me a lot. I'm new in android and web sorry.
What I personally did for one app was that I updated my device database whenever the user went online. I saved the current database version using SharedPreferences and then the app checks the servers' database version -- which was saved in a text file for easy reading and parsing by the Android Device. If there's a mismatch, the device downloads the database update, essentially performing a sync process. It downloads the entire database (if done properly, it shouldn't be too much) and photos if need be.
Basically what happens is that your app will check (once it goes online) if there's a database update available.
this link might be helpful sync remote mysql db to sqlite
Just save a time stamp on the device of when was the last time the offline database was updated.
Do the same each time for the online database - each time the user adds something from the web, just save a time stamp.
So, when you launch the application, just compare the two time stamps. If the time stamp of the online database is greater than the one on the device - sync the new items.