28

I am using Android Room Database for creating the database for my android app. It works perfectly for me locally but I cannot link to a server to have it online as well. I am using Firebase for Authentication. I am trying to use Firebase Realtime Database to save the whole database object from Room and load the correct database on app startup according to the authenticated user.

I want to ask if this is possible at all? and if I can just save a whole instance of Room database or I need to re-create the database on Firebase and save my data item by item?

I also can't seem to be able to get access to the database data of Room, as when I get an object of the AppDatabase class it doesn't really pass the data. And I don't know how should do the opposite, to assign the data retrieved from Firebase later to the local data saved?

Also if it's not possible with Firebase, do you have any recommendation for some other server I can use with Room?

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Pierre Ghaly
  • 777
  • 2
  • 7
  • 17
  • Hi, Pierre Ghaly! I have been searching for weeks how to tie together **Android Room Database** and **Firebase Realtime Database**, but I've only found separated examples. Can you give any suggestion on how to accomplish this task? – Aliton Oliveira Jul 02 '19 at 21:25
  • Hi Aliton! According to my research (I am not sure I am still up to date), but there was no way to tie them together. As I did (according to my answer below) I just re-created the database from scratch using Firebase. – Pierre Ghaly Jul 27 '19 at 20:45

2 Answers2

18

After a lot of researches and looking desperately for an answer here's what I reached:

  • Firebase already got a straight forward way to create the database and host it online. (I had my database already created so was trying to save time, but creating it from scratch using Firebase Realtime Database was a lot faster)
  • Room Database is quite perfect if you are planning to save your database locally and offline (Up to the post date)
Pierre Ghaly
  • 777
  • 2
  • 7
  • 17
  • 2
    Remember, Firebase Realtime database also offers persistence (offline) storage which works seamlessly like Room database. Add below line in your Application class [Manifest file -> Application -> android:name] to initiate this process at once. `/* Enable disk persistence */ FirebaseDatabase.getInstance().setPersistenceEnabled(true);` – PravyNandas Jul 07 '18 at 12:55
  • 1
    @PravyNandas Yea I found out that this was an option too, it was quite useful for me since I needed online and offline working together! However, Room was more easier for me to use when I needed only an offline database! Thanks for pointing it out :) – Pierre Ghaly Jul 09 '18 at 21:33
  • 4
    I've got a similar task. I'm going to use Room as local storage, but I need a sync/backup functional too. For this purpose, I'm going to use replication log in Firebase. It's a single collection with "flat" representation of a Room's data. – Alex Shevelev Nov 19 '18 at 07:47
  • That would be a good option definitely if you just need a flat representation. Thanks for sharing! – Pierre Ghaly Nov 19 '18 at 13:17
  • Hi @PierreGhaly, I am in the same situation and came across your post. Currently I'm using ROOM Sql database, DAO's, RxJava Observables with LiveData. When you switched over to Firebase how did you deal with this issue? – Byron Aug 07 '19 at 15:21
  • Hello @Byron. I first tried to immigrate everything (which is what you're trying to do now I believe), but then I realized it's easier to just re-create Firebase using its tools from scratch. Although it might seem like a waste of time but for me it was totally a time and energy saver! - I hope this helps – Pierre Ghaly Aug 07 '19 at 17:31
5

I think their is no need to connect your Room Database with Firebase Realtime database because,okay first try to solve this problem, why we use Room Database ?because when we don't have internet, we can get data from Room Database , no need of network... Firebase also provides offline mode , it can also save data in persistence, so why we use Room Database ?.. Hope you got the point...

Jayant Kumar
  • 775
  • 5
  • 12
  • Welcome to stackoverflow, please read through the question and accepted answer for better details. – Sahil May 13 '20 at 06:16