Realm Sync快速的例子

时间:2017-03-29 15:32:32

标签: ios swift synchronization realm

大家好我试图创建一个使用Realm对象服务器同步数据的ios应用程序 但是有些东西我不见了

我在我的服务器上安装了相应的Realm对象服务器(远程) 我可以正确访问仪表板

我可以使用Syncuser登录

但我有同步数据的问题..当我打开应用程序我登录用户但之后(我无法同步任何)

我在网上搜索了一个完整的例子但没有成功 任何人都可以建议一个简单易用的示例,使用realm sync与swift?

由于

1 个答案:

答案 0 :(得分:1)

这是我使用RealmSwift的聊天应用程序的实际实现。我使用了一个名为Message定义的模型对象:

class Message: Object {
    dynamic var userId = ""
    dynamic var name = ""
    dynamic var text = ""
    dynamic var dateCreated = NSDate()
}

接下来,我使用创建了一个控制器来发送和同步消息:

// Instance variables
var messages: Results<Message>!
var token: NotificationToken!
var configuration: Realm.Configuration!

override func viewDidLoad() {
    super.viewDidLoad()

    // Your viewDidLoad implementation

    self.setupRealm()

}

接下来是Realm的设置方法。如Real Documentation中所述,如果Realm具有只读权限,则必须使用异步打开领域中所述的asyncOpen API。在没有read-only的情况下打开asyncOpen领域会导致错误。

private func setupRealm() {

    let serverURL = URL(string: "http://your.server.ip:9080")!
    let credentials = SyncCredentials.usernamePassword(username: "example@mail.com", password: "password")

    SyncUser.logIn(with: credentials, server: serverURL) { user, error in
        if let user = user {
            let syncURL = URL(string: "realm://your.server.ip:9080/~/Message")!
            let syncConfig = SyncConfiguration(user: user, realmURL: syncURL)
            self.configuration = Realm.Configuration(syncConfiguration: syncConfig)
            self.setupDataSource()

        } else if let error = error {
            // handle error
            debugPrint("error: \(error.localizedDescription)")
        }
    }

}

private func setupDataSource() {

    Realm.asyncOpen(configuration: self.configuration) { realm, error in
        if let realm = realm {
            // Realm successfully opened, with all remote data available
            self.messages = realm.objects(Message.self).sorted(byKeyPath: "dateCreated", ascending: true)
            self.token = self.messages.addNotificationBlock({ (changes: RealmCollectionChange) in
                debugPrint("Message update event")
                self.collectionView.reloadData()
            })

        } else if let error = error {
            // Handle error that occurred while opening or downloading the contents of the Realm
            debugPrint("error: \(error.localizedDescription)")
        }
    }

}

deinit {
    self.token?.stop()
}

最后我用来发送消息的sendMessage()方法:

func sendMessage(id: String, name: String, text: String) {

    let message = Message()
    message.userId = id
    message.name = name
    message.text = text
    message.dateCreated = NSDate()

    Realm.asyncOpen(configuration: self.configuration) { realm, error in
        if let realm = realm {
            // Realm successfully opened, with all remote data available
            try! realm.write() {
                realm.add(message)
            }
        } else if let error = error {
            // Handle error that occurred while opening or downloading the contents of the Realm
            debugPrint("error: \(error.localizedDescription)")
        }
    }

}

希望它有所帮助! 再见!