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

房东的猫 提交于 2019-12-09 12:40:09

问题


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?


回答1:


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.




回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/37903267/androids-realm-io-how-to-sync-with-server-side-mysql-db

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!