How to modify firebase offline data via android? - 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.

Related

Firebase Cache vs Implementing a Custom Room Cache

We are using firebase realtime database and I was thinking about implementing a cache locally to reduce repeated calls.
So I came up with an algorithm which involves room persistence library.
stream only the latest data from firebase
store in room cache
when requested fetch all data from room cache and return
But then I started thinking about the cache that firebase provides and started realizing that I might be able to avoid room library at all.
stream only the latest data from firebase
when requested call fetch data using a singleValueListener
Since we are using single value listener only the cached data would be fetched from firebase.
What are the drawbacks of using the second approach? I know that firebase cache is limited to 10MB so that might be one
The one big thing you need to know here is that the cache managed by the SDK is almost fully outside your control. You can set the size of the cache, and you could clear it by trying to find the database file it uses, but otherwise, you can't configure it.
If you write code on your own, will have to make every decision about how it works, and it will be a lot of code to get everything right.
Unlike in Cloud Firestore, where offline persistence is enabled by default, in Firebase realtime database to enable offline capabilities you need to use the following line of code:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
This means that you'll have by default a local copy of your database. So there is no need to add another one. So in this way you'll only get new data, otherwise everyhing is you get is from cache.

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.

How Secure is Firebase Local Database in Android?

I am planning to switch to Firebase as my local and online database for my Android app. As per the docs, Firebase stores changes to the local database first and then pushed it to the online DB when network is available.
In my app, I would be putting some really sensitive data about the user in the database. So here are my questions,
How secure is the local Firebase database?
How difficult is it for a well-intentioned hacker with the right tools to hack it?
Is it just a simple JSON file like the online database, which anyone with root access can open?
Thanks.
In a general sense, Firebase Realtime Database can be used while offline. However, the expectation is that the app is supposed to be connected most of the time, and changes to the database that happen while offline will be synchronized when it has connectivity. 100% offline use is not really a supported use case, because the canonical data store is on the server.
The local copy of the database is limited to (10MB, at least on Android this is the case). If you intend to write to the database beyond this limit while offline, it will evict part of your cached data to make room for whatever you’re adding. Then, you will no longer be able to read those evicted values until the app goes back online. Worse, managing a growing list of writes to apply when back online is taxing on the app, so you don’t want to plan a lot of writes while offline.
Also, if you have permissions or validations defined for your database, these can only be checked on the server. So, if you’re doing offline writing to your local cache and you no longer have an active listener, you may never know if those writes fail.
Because of these caveats, it’s better not to think of Firebase Realtime Database as an “offline” database. It’s better to think of it as a “synchronized” database that actively syncs to the server while connectivity is present.

manage non local sqlite database in android

I have started working with databases lately, I was able to create a local database and manage it in my Android app. Now I want to move to higher level with it but I don't know how to do it.
The users in my app need to be able to modify the database, for example insert new data in it. When a user insert new data in the database I want other users to see this change in their copy of the database.
I understand that I will need to store that database on a server or something and synchronize it with the users.
Can anyone tell me the steps to do so?
You should perfom this task in steps.
First, make the local database, and use a system to know when/what changed.
I usually work with triggers myself, but any "mark" is enough to synchronize.
Then, you must make a replica of that database somewhere else. Realise that maintaining the databases is a process, any change in the structure of one database must be performed in all other as well.
Finally, you must implement a method to transfer the data.
So, for an example:
db_local the database in the device.
db_outside the database in the internet.
db_local.trigger -> onInsert
On the applications, check for internet, then connect to your server, then upload the same command to db_outside and run it...
In this step, you must handle connection issues, and if the SQL command was succesfully executed, you have replicated the database content.
Once you have the replicated database, inform a system (like google cloud messaging), that the database was changed, and have the other users pull the info.

Mobile DB app off line from internet

I have an architecture question. If you have a web app that is storing information on a DB server, theoretically, I should be able to use the middle tier logic for a mobile app. When the mobile app starts it can connect and populate a local SQLite DB or use JSON to store information within the mobile app. What if the mobile app also needs to work in off-line mode? Do you have it sync the next time it is connected? Do you have the mobile pull down and populate a complete DB or so it available in off-line? What are the best ways to architect a mobile app that has to go from on-line to off-line?
The simplest solution would be to put a "LastEdited" column into every table in your database and then pull query all the data which has updated since the last sync ( and you can perform a check on the index to detirmine if you need to update or insert into your own local cache. )
The ability to delete rows should actually be limited to a boolean "isDeleted" flag in this case to keep the sync process nice and simple.
If you have then the ability to edit or create rows from your app then you should keep a local table of changes to sync when you can go online and may have to implement some form of "merge" logic.
Several things you need to consider.
If your app is read only, you should implement a 'delta sync' logic in your local d. Keep a timestamp of last sync and get updates from your server. Of course, you need to consider the local db size in getting too large.
If you app is read/write, when working offline, you need to consider the two way sync especially when same record can be updated in different devices/users.

Categories

Resources