max
max

Reputation: 623

Android's realm.io how to sync with server side MySQL DB

I am working on an Android app using realm db as my local db, how do I sync my records in realm with my server side MySQL db?

Upvotes: 7

Views: 7808

Answers (3)

Malte
Malte

Reputation: 561

As @Alexander already said, you cannot automatically sync your local Realm db with a server-side MySQL db. However, you can implement a straight-forward custom solution which uses a server-side REST API to manifest changes of the local database.
You basically just observe your local database for changes (which is really easy in Realm) and call the REST API accordingly when something changes. A SyncService implementation could be something like this:

class SyncService {
    private let realm: Realm
    private let tokens: [NotificationToken]

    init(modelTypes: [Syncable.Type], realm: Realm = try! Realm()) {
        self.realm = realm

        tokens = modelTypes.map { modelType in
            modelType.registerNotificationObserver(for: realm, callback: SyncService.handleUpdate)
        }
    }
    ...
}

You can find more information in this article where the author implements a one-way sync using Realm with a simple REST API

Upvotes: 1

Zhanibek Bekbossyn
Zhanibek Bekbossyn

Reputation: 11

Realm is syncing with server side scripting Node.JS 4.x. And it's not free. Event handling, server-side data access available in Professional/Enterprise Edition. In server-side you may add change listiner, then to RDBMS. Look at https://realm.io/docs/javascript/latest/#event-handling

Free way

You may add on Realm.addListener when it change send to server use WS/HTTP Request. But if application offline you need more logic

Upvotes: 0

Alex
Alex

Reputation: 616

Update from 27 Septermber 2016:

Realm now fully supports synchronizing data against Realm Object Server. More details are available here.

19 June 2016

There is no such way - the Realm doesn't have such mechanisms, and it is logical. You mix different approaches - syncing data and storing locally (the mission of Realm, SQLite and other Android mobile DBs). You can do it by your own custom implementation (like in the tutorial), or using by SyncAdapter.

Upvotes: 8

Related Questions