Firebase online to Android offline datastore - android

I am building a mobile app to allow for real time messaging, befriending users, creating groups to both chat and share images with, as well as creating events where users can invite one another.
I have chosen to use Firebase as the online back-end. But, given Firebase uses a NoSQL data model, while Android SQLite uses SQL, when saving data offline in Android what is the conventional way to handle this? Is there a simple way to convert or simply save from NoSQL to SQL, or do I need to build a converter?
(This is especially important for the events, as once created, they must be scheduled in the AlamManager, giving users alerts upon event time)

While you could implement your own solution, truth is, you do not need to build anything from scratch.
There are free Android libraries which could help you. I would recommend you:
SimpleNoSQL
The transition from Firebase to SimpleNoSQL is pretty straight forward, and is mostly the same as if you were using any form of SQL:
1) You get the data from your remote db: you can get this trough a request to your remote server, it doesn't matter what language you are using as long as it can return a response you can catch.
2) You save said data to your local NoSQL db: once you have the information requested, it is up to you what to do with it. You could save it to a TXT file, a SQLite db, NoSQL db, save it to the SharedPreferences, etc.
Hope that helps.

Related

What is the best way to store app data and load it in app?

This is just a information question. I'm new to Android app development and currently I'm working on my first app and and it is ready for the release. Now I'm concerned about how to handle heap of users and where to save all their details my app is a service booking app so it needs to save all the order details products details and lots other stuff.
Currently I'm using cloud firestore to load and save all the data of app. But I'm having some issues like without authentication it won't allow users to access some of my data and other. I wonder how large apps save their data and load them perfectly.
I wish someone will help me how can I save all my app data and load them perfectly in app. And suggest me for a best way to manage large user base. And other stuff.
First of all, firestore is good option if you don't have complex backend logic on the database. For simple CRUD operations on data firestore is a good choice but as you said you have a bulk of data then you must go for the Backend database and then connect your database with Rest API. So that all your complex queries will be done on the backend and you can simply consume your API in the app.
If you have lots of data from different users, maybe you should use a central server(DB), something like Postgres or MySQL should work fine.
At the same time, you can also do some sort of caching to accelerate the fetching process, like create a small database locally(you can use Room) to store some user specific data.

How to modify firebase offline data via android?

The android app I am currently developing will mainly run offline. However while online, the users can connect to the DB to download the data they need. Furthermore they are able to manage the data offline, so they can remove the data they don't want to use. The users cannot modify the online database.
I am using Firebase with its Persistence enabled feature, to save the date for online use. My problem is, that I just cannot manage the data properly.
Since firebase downloads the whole database:
Is there a way to choose, which queries I want to download form firebase?
If not, can I delete those queries I don't want to use, from local content?
It looks like you don't want the sync feature of the firebase database.
However while online, the users can connect to the DB to download the
data they need. Furthermore they are able to manage the data offline,
so they can remove the data they don't want to use. The users cannot
modify the online database.
set write as false for this table in the rules and the modification will not be possible, since you have enabled disk persistence firebase will read the newer updates but wont write the user changes. Also it might discard the existing changes of the user(which the user removed) during the sync and put the newer ones from the cloud, so you might(as I have never tried this) end up doing the sync yourself, please search and read up on this.
Since firebase downloads the whole database: Is there a way to choose,
which queries I want to download form firebase? If not, can I delete
those queries I don't want to use, from local content?
You will have to turn off on the sync on the each of your table like shown below
DatabaseReference dbRef=FirebaseDatabase.getInstance().getReference("table-name");
dbRef.keepSynced(false);
Refer here in the docs. I think this has to be done on each node as there is no way to turn it off on the entire firebase realtime DB except disable disk persistance which you want in your case.
IMHO it looks like you are using firebase as a place to store your data in the cloud, in case the sync feature ends up discarding your local changes to sync the online ones you should consider using SQL-lite DB with Room ORM as it will serve your purpose of just fetching the data and modifying it locally, however the tradeoff here is that you will have to maintain the data in a proper manner to query the newer changes or see if there is any updates/deletes on any of the older records.

Use Firebase DB with local DB

In my app I have SQLite db. I want to introduce sync between devices of my users. Firebase DB looks like an acceptable solution, but Firebase DB is cloud db at first. So, I can't use it as local db if user will reject auth dialog and let him use app, but without cloud-sync.
Now I think about combining my local SQLite db with cloud Firebase db.
For example, when user adds new row to local SQLite db, my app will also put data into Firebase DB. Other devices of this user will catch this event and update their local db. When the user uses authentification and installs app on new device, I want it to download all rows and put them into local SQLite db. That's my idea: use Firebase DB only for synchronizing data, not for storing it at device. Main reason for it is to let user use my app without authentification&synchonization. The second is that Firebase DB is not designed to be used as local db.
I'm right? Is it okay to use Firebase DB with another local DB?
Related question:
link He want the same as I want:
my plan is to offer the user the option to stay offline
If your firebase structure is not too complex you could also make a interface which defines methods like
void addData(Data data);
Data getData(long id);
void editData(Data data, long id);
void deleteData(long id);
then create 2 classes implementing that interface, one using Firebase the other using SQLite.
DatabaseImplementation
FirebaseImplementation
Inside your Firebase implementation, you would publish the data like normal, and publish one new node to something like root/requestUpdate/userId/push/ and push would contain information on where you request an update, and what deviceId published it.
Then have a ValueEventListener tied to that mentioned node, and if it gets a new child, have it look whether the deviceId is the same or not. If it is not, have the FirebaseImplementation getData using the information you got, and then use the DatabaseImplementation, to addData.
That would make sure that whenever a change is made, any other logged in client will know to update its firebase. If the client is not online, the next time he will be online he will do it as ValueEventListener triggers when it is attached. Make sure to loop through all the requested updates to make sure all are made. Also store the push keys of any updates you did complete on a local database that way you dont end up updating more than once.
Basically the firebase will always be up to date and store any changes a user made on a seperate node which is listened to by all clients.
Obviously this solution still has many problems you would need to fix, like figuring out when to delete the requestUpdate node. Logically after every user has synced but how do you determine this? ...
As for the first login, you would need to write a populateDatabaseFromFirebase() method which will do a whole lot of getDatas and addDatas. How you would do that will depend on how your DB looks. You then would store that the user has already logged in with SharedPreferences and the firebase UID.
That all being said, this will only work if your firebase is pretty flat. If you have a complex database, then everything becomes much more complicated and entangled and then it might be worth looking into an external library.
Some options for HTML5 hybrid apps
This is not what the OP asked about, but hopefully useful to some seekers.
You can use any combination of client and server database to implement storing remotely-maintained data in the device so it will be available when offline.
Some client options :
SQLite
(which is using the "native" browser database, works on iOS Safari
and Android webkit browsers)
IndexdDB
(another "native" option, but not supported in early Android, or
fully supported for iOS - so NOT a good option)
JayData
(which provides an abstraction layer from the underlying native implementation)
Lawnchair
(another popular client abstraction - I found the documentation lacking and have not used this for that reason)
Some server options :
MongoDB
RethinkDB
MySQL (for an SQL DB on the server)
and, of course there are many many more.

Realm.io offline, Parse online - What about users

I'm using Parse for the first time to play around with.
I use Realm.io as a offline db.
Both are very powerful and it's great to get to know them better.
(Big up to both teams!)
My app should be able to be used on remote locations, where you can't get data from Parse. I want to be able to rely on Realm in these situations.
I just implemented user management in Parse, but I don't know what would be the best way to reflect this with realm. Should I create a User object in realm and make a one to one relation with all my models. Should I save the username as a property in the models?
I also tried the combination of Realm and Parse. And yes you'll have to sync the data manually between both datasets by creating models for each.
But I think writing (almost) the same model classes for both Parse and Realm could be counter-productive. That's why I dropped Realm and used Parse LocalDataStore instead. By using localDataStore, you still would have to write the sync logic yourself but the code would be cleaner (in my opinion).
Another option is to go for Firebase. Firebase handles the sync logic itself so you don't have to and it even gives you real-time sync. So I would highly recommend you check that out. But Firebase doesn't have Push Notifications. For that you can still use Parse.
Hope this helps.
Update
Firebase now offers push notifications as well. (and Parse has been discontinued)

approach for synching android app database with server db?

I'm developing an Android app as a "proof of concept" for our company. If they like it and think it's worth investing, then we'll move on to bigger things. I'm trying to figure out the best/most practical approach for this.....the basics of the app will connect to our DB and display information regarding a specific customer. For now, let's say we will only pull data from 3-4 tables (but there could be 10+ in the future). If the app doesn't have an internet connection then it should use the local DB. What is the best approach for this? Here's what I was thinking and would like some input/suggestions if possible:
1.) app runs checks internet connection. If exists, check db version (how, through a web service?)..if server db is newer, get latest data. If no internet, use local db.
2.) app parses data and displays it.
If this is correct, then there could be no modifications to the web service that would add fields to a result without changing the app as well. Is there a way for an app to parse fields regardless of how many fields there are?
I've read and walked through the tutorial on google with databases and such (Notepad tutorial) but it seems like the column names are all hard-coded in the parsing class, which I was hoping to avoid.
Sorry if this is confusing but I know I need my app to use a local db to read data, I also know that the app must get data from the server when it can (via onCreate or a refresh button) and copy it locally....Copying it locally is the part I'm having trouble understanding I guess....is there no way of saying "go out and get this result and display it", knowing that those results could mean 5 fields the first time or 1 the next.
Any help/guidance is greatly appreciated!
You probably want to use a SQLLite DB to store your data locally, a ContentProvider to provide CRUD access to the db, and a SyncAdapter to sync with your server when possible. The Sync Adapter also writes to the DB via the ContentProvider. See the SampleSyncAdapter sample in the SDK for an example of how this works. You will be implementing your own ContentProvider, but the sample just uses Android's supplied Contacts ContentProvider.
http://developer.android.com/resources/samples/SampleSyncAdapter/index.html

Categories

Resources