Data storage in Firestore For Offline App - java

I am using Firestore as a NoSQL databse in my Android app. I want to store data locally in my Android device and avoid syncing with Firestore server in the initial stages of app. Is is possible for the data to persist on device even if app or mobile device restarts without syncing with Firestore database? If yes then how do I do it ?

I want to store data locally in my android device and avoid syncing with Firestore server in the initial stages of app.
According to the official documentation regarding Firestore offline persistens:
For Android and iOS, offline persistence is enabled by default. To disable persistence, set the PersistenceEnabled option to false.
And to answer the second question:
Is is possible for the data to persist on device even if app or mobile device restarts without syncing with Firestore database?
Yes it is. This kind of persistens is called disk persistence, which means that recently listened data (as well as any pending writes from the app to the database) are persisted to disk. The data in this cache survives app restarts, and phone reboots.

Related

How to sync SQLite database with firebase?

My application uses the SQLite database, but I want to offer the user to access their data on different phones if they want. I don't want to drop SQLite and instead start using Firebase because Firebase needs registration and authentication which for some users is not preferable. The users should have an option, whether they want to register and use firebase or stick to the offline SQLite database.
If the user was offline or logged out of Firebase and then made changes in the data like (add, delete), when the user logged in or backed online, I want the changes to reflect on the Firebase too.
What are some steps that need to be done to accomplish this?
I don't want to drop SQLite and instead start using firebase because firebase needs registration and authentication which for some users is not preferable.
If an authentication mechanism with one of the providers is not preferable, then you should consider implementing an anonymous authentication. What it basically means, it allows you to create an anonymous user, without having to ask for any information.
The users should have an option, whether they want to register and use firebase or stick to the offline SQLite database.
While this mechanism can be implemented, I cannot see any reason why would you do that, since both, the Realtime Database and Cloud Firestore, have their own offline persistence mechanism. The latter, for Android and Apple platforms, offline persistence is enabled by default.
If the user was offline or logged out of firebase and then made changes in the data like (add, delete), when the user logged in or backed online, I want the changes to reflect on the firebase too.
That's what the offline persistence mechanism does. While offline, all operations are added to a queue, and once the device regains connectivity, all operations are synchronized with the Firebase servers.
What are some steps that need to be done to accomplish this?
In the case of Cloud Firestore, none. In the case of the Realtime Database, simply enable it using this line:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);

Offline Sqlite sync between two devices

I developed an android App which uses a local Sqlite Db on the Devices. Now I want to sync the database with the database on a second device. The devices are never connect to the internet. Is there an "easy" way to synchronize the db via bluetooth or nfc? So far I've only heard about the online synchronization with MySQL?
Thanks

Android: Linking and syncing Room database to online server database

I am developing an app that uses a database to store the data on the server, but I am trying to save some of the data locally, and in the event of no internet connection being established, save new data locally to the device and synchronize any changes to the server when an connection is re-established. What is the best and most efficient way to do this?
I have been looking at Androids Room persistence library and it seems like the logical choice, but I am not sure how it goes about synchronizing changes to/from the local storage database. I have looked at multiple threads and forums for help, but have had no luck so far. Please help.
One way is to build your own sync adapter: https://developer.android.com/training/sync-adapters
You will need to handle most of the sync logic between the client and the server, but that allows you to use any database technology in the server. From the docs:
A sync adapter doesn't automate any data transfer tasks. If you want to download data from a server and store it in a content provider, you have to provide the code that requests the data, downloads it, and inserts it in the provider. Similarly, if you want to send data to a server, you have to read it from a file, database, or provider, and send the necessary upload request. You also have to handle network errors that occur while your data transfer is running.
A sync adapter doesn't automatically handle conflicts between data on the server and data on the device. Also, it doesn't automatically detect if the data on the server is newer than the data on the device, or vice versa. Instead, you have to provide your own algorithms for handling this situation.
Use firestore and enable offline data persistence. https://firebase.google.com/docs/firestore/manage-data/enable-offline

Firebase Database Not Syncing Properly

I'm trying to make a chat application. For that I'm using firebase database. And to show messages using FirebaseRecyclerAdapter.
mNewAdapter = new NewChatRecyclerAdapter(
ChatMessageItem.class,
R.layout.message_item,
RecyclerView.ViewHolder.class, ref.limitToLast(300).orderByChild("timestamp"), isAMod, publicroom, Fusername, booIsPrivateChat, chatid, otheruser);
And for syncing,
ref.limitToLast(300).orderByChild("timestamp").keepSynced(true);
But everytime I reopen the app it takes 3-5 seconds to load all new messages.
So how can I fix this problem..
Thanx in advance...
The Firebase Database only synchronizes data while there is an active connection to the server. This normally only happens when the app is active, since Android closes the connection when the app is inactive.
While there is a connection, the Firebase SDK normally only synchronizes data in a location when you have an active listener in that location. When you call keepSynced(true) on a location, it will also synchronize when there is no active listener in the location.
Calling keepSynced(true) is most useful for the main list in your app, such as the data you show in your main activity. By keeping that synchronized, it will also be updated when your users are in a different screen. That way, when they come back to the main activity, the data is immediately available.
To reduce the time the user has to wait when restarting the app, you can tell Firebase to store its cache to disk by calling setPersistenceEnabled(true). Since loading from disk is typically faster than loading from network, this may speed things up.
Enabling Offline Capabilities so as your app will cache those data
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Enabling Offline Capabilities

Parse.com: Does PinInBackground() to Local DB save to the Cloud eventually?

I use the Parse.com Cloud service in my Android app to sync data between devices.
I use the app mainly offline and use the local DB
Parse.enableLocalDatastore(getApplicationContext());
I would like to use PinInBackground() to store the data locally and sync once every day by hitting the sync button in my app.
Now if my app crashes or the device restarts - how do I know which objects are stored only locally so I can sync them with Save()?
pinInBackground() will not save to the cloud eventually. The only action that does BOTH is saveEventually(), which will pin your objects locally if your network is down so that you can still query them even though you are still offline.
More in the Parse Android guide

Categories