realm / realm-java

Realm is a mobile database: a replacement for SQLite & ORMs
http://realm.io
Apache License 2.0
11.46k stars 1.75k forks source link

I have the question. How I can use database realm for Android and Firebase server #4896

Closed miketrinh1995 closed 7 years ago

miketrinh1995 commented 7 years ago

I have a Android project, using realm to build the database. I need a server with a real-time database. How I can do that. Give me a solution. Thank you!

Zhuinden commented 7 years ago

Well it is possible to make Realm into a real-time database if you run a Realm Object Server somewhere and you enable sync mode and use a SyncConfiguration ( see here )

miketrinh1995 commented 7 years ago

@Zhuinden Thank you! So, I have to buy a server or use a virtual machine, I'm using Windows OS ?

Zhuinden commented 7 years ago

Realm Object Server (ROS) runs on Mac OS or Linux.


But of course, if you can make Firebase do what you want, then you could use Realm Mobile Database with it as you normally would. I haven't done that before because I don't use Firebase, so I can't help with that.

miketrinh1995 commented 7 years ago

@Zhuinden I'll try Realm Object Server. Thank you so much!

miketrinh1995 commented 7 years ago

After 1 day, I know how to connect Realm Database with Firebase Database server. It's working perfectly like Realm Object Sever. If anybody has the same problem with me, contact me.

trevor-sonic commented 7 years ago

@trinhvanminh2009 hi I'm wondering how you do this, can you do with cloudKit as well?

miketrinh1995 commented 7 years ago

@trevor-sonic I have no ideas about that. I'm using Firebase Database instead of use Real-object! About cloudKit, I'm an android developer, I don't know anything about IOS developer!

magician20 commented 7 years ago

@trinhvanminh2009 can I know how u did it ? or, can u give me the material that help u to do it ?

miketrinh1995 commented 7 years ago

@magician20 It's a lot of steps to do. If you want more detail. Contact me on Facebook https://www.facebook.com/minh.trinh.52493

nullbringer commented 6 years ago

@trinhvanminh2009 It would be great if you could write a gist explaining the steps and share it here. Cheers

magician20 commented 6 years ago

ye I hope Too:).

miketrinh1995 commented 6 years ago

@urbangeek @magician20 The simple ideas are:

  1. Connect Firebase and using Realm-Object
  2. You have to know which one save on Firebase, which one save in local.
  3. Only save key or primary key in local to easy to query on Firebase I'm non-native speaker speak English. Hope you guys understand. Any question just lets me know.
juanmendez commented 6 years ago

This is how I did it. I wrote an interface which does crud operations for a table. So one implementation did initially Realm, including opening and closing it. Next I wrote a new implementation which this time embedded the first one, and there I kept updating Realm, and updating Firebase. Since I mentioned I wrote opening and closing, I could start and stop watching live changes in Firebase. So the live updates helped me to add/update/delete my realm objects. In this situation your app will rely on the Firebase implementations not knowing about it.

If you already started using Realm, you can just check if the Firebase is empty meaning it's time to push all Realm data into Firebase. In contrast if you installed an app in a new device, and you already have data in a Firebase object, then the live event will keep popping letting you know of each new object, so it's time for Realm to add each one of them.

In my case I had already a Realm class which used a primary key as integer. Since the data is not shared with other users, I set it as a string key in Firebase. Lets say you have a free version of your app, and the paid version is going to be hooked into Firebase, I would recommend applying a self generated string as primary key. In this way you are doing the same thing Firebase does, but without using Firebase initially. I hope this gives some insight.

I also want to share that in my case a Realm object threw an exception in Firebase meaning it couldn't use serialized objects. So a solution was to write a utility class which cloned my real Realm object, and that was it for Firebase. Maybe I should have used instead realm.copyFromRealm(realmObject)