Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 904
Alexandr Avatar asked Aug 19 '16 00:08

Alexandr


1 Answers

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.

like image 60
Linxy Avatar answered Nov 05 '22 12:11

Linxy