使用Swift 3和Realm同步Apple Watch和iPhone [英] Synchronizing Apple Watch and iPhone using Swift 3 and Realm

查看:141
本文介绍了使用Swift 3和Realm同步Apple Watch和iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Apple Watch和iPhone上显示和修改我的数据结构.

I need to Display and modify my data structure from both Apple Watch and iPhone.

数据库:

我目前正在使用一个简单的领域结构,其中有一个对象A和一个可以容纳很多A的对象B.

I am currently using a simple Realm Structure where I have an Object A and an Object B which can hold lots of A's.

因此,在iPhone上,用户可以创建B并添加A,当然还可以查看所有A和B.

So on iPhone the user can create a B and add A's and view of course all A's and B's.

我希望Apple Watch能够显示当前B的所有A,并为用户提供将新A添加到当前B的机会.

I want the Apple watch to show all A's of the current B and give the users the chance to add new A's to their current B.

我尝试执行的方式:

我想将Realm文件从iPhone移到手表上或以其他方式移动. (这是来自互联网的提示)

I wanted to move the hole Realm file from iPhone to the watch or the other way. (That was a tip from the Internet)

iPhone代码:

    override func viewDidLoad() {
    super.viewDidLoad()


    if WCSession.isSupported() { //makes sure it's not an iPad or iPod
        let watchSession = WCSession.default()
        watchSession.delegate = self
        watchSession.activate()

        transferRealmFile()

        if  watchSession.isWatchAppInstalled {
            do {
                try watchSession.updateApplicationContext(["foo": "bar"])
            } catch let error as NSError {
                print(error.description)
            }
        }
    }
}
func transferRealmFile(){
    if let path = Realm.Configuration().fileURL {
        WCSession.default().transferFile(path, metadata: nil)
    }
}

WathcKit扩展:

WathcKit Extension:

  func session(_ session: WCSession, didReceive file: WCSessionFile) {

    //set the recieved file to default Realm file
    var config = Realm.Configuration()
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    let realmURL = documentsDirectory.appendingPathComponent("data.realm")
    if FileManager.default.fileExists(atPath: realmURL.path){
        try! FileManager.default.removeItem(at: realmURL)
    }
    try! FileManager.default.copyItem(at: file.fileURL, to: realmURL)
    config.fileURL = realmURL
    Realm.Configuration.defaultConfiguration = config
}

然后,每次我写入Realm时,我都会调用transferRealmFile().这可行,但我无法解决此问题:

Then I call transferRealmFile() every time I write to Realm. This works but I can't solve this Problems:

问题:

  1. 如果仅启动watchKit App,则不起作用.
  2. Apple Watch到iPhone的工作方式不同. (我想我需要更改didRecived代码,但我不知道是什么)

问题:

您知道由谁来解决这2个问题,或者您是否知道一种更好的方式来处理这种情况,或者WathcOS 3中的iPhone Watch之间的交互方式会改变吗?

Do you know who to solve this 2 Problems or do you maybe know a better way to handle the situation or will the way we interact between iPhone an Watch change in WathcOS 3?

推荐答案

使用watchOS1,可以使用AppGroups在iOS应用及其Watch扩展之间共享资源(甚至是您的Realm数据库).但是,Apple在watchOS 2中删除了此功能,因此现在在iOS和watchOS应用程序之间共享数据的唯一方法是通过WatchConnectivity.看看答案.

With watchOS1 it was possible to use AppGroups to share resources (even your Realm database) between an iOS app and its Watch extension. However, Apple removed this in watchOS 2, so now the only way to share data between your iOS and watchOS apps is via WatchConnectivity. Have a look at this answer.

遗憾的是,WatchConnectivity框架要求WCSession在两个设备上均处于活动状态以传输数据,因此您无法真正解决问题1.

Sadly the WatchConnectivity framework requires the WCSession to be active on both devices for transferring data, so you can't really get around problem 1.

在我看来,仅传达两个应用程序之间的更改而不发送整个Realm文件是一个更好的解决方案,因为您的Realm文件可能会变得很大,因此向前和向后发送它会花费很多时间,资源,而只是发送更改应该会更快.

In my opinion it is a better solution to only communicate the changes between the two apps and not send the whole Realm file, since your Realm file can get quite big and hence sending it forward and backward can take a lot of time and resources, while just sending the changes should be way faster.

这篇关于使用Swift 3和Realm同步Apple Watch和iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆