与WatchOS共享领域数据 [英] Share Realm Data with WatchOS

查看:90
本文介绍了与WatchOS共享领域数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我想同时将一个Realm数据库与iOS 10应用程序和watchOs 3应用程序一起使用.因此,我要做的是将框架添加到针对三个不同目标的嵌入式二进制文件中.碰巧效果很好,但是watchKit扩展似乎无法识别我在iOS环境中创建的对象.在这两个设备之间如何共享一个领域数据库?

解决方案

更新: 好的,感谢 chrisamanse 的单挑,我对此进行了更多研究.

事实证明,在watchOS 2上不再可以使用应用程序组".它们现在是两个孤立的进程,没有任何共享资源.

因此,这意味着有必要在手表和手机上维护一个单独的Realm文件,并通过

值得庆幸的是,可以使用iOS的应用程序组"功能来指定一个共享文件夹,父iOS应用程序和watchOS应用程序都可以访问该共享文件夹,并且可以读取和写入其中的任何Realm文件./p>

在应用中启用App Groups权利后,只需将Realm文件的位置设置为指向该位置即可.

let sharedContainerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.my.appgroups.bundleid")!
let realmURL = sharedContainerURL.appendingPathComponent("SharedRealm.realm")

let realmConfiguration = Realm.Configuration()
realmConfiguration.fileURL = realmURL
let realm = try! Realm(configuration: realmConfiguration)

Realm网站上有教程解释了如何使它更详细地工作,但是API和Swift版本目前已过时.

In my project I want to use one Realm Database with my iOS 10 application and my watchOs 3 application at the same time. So what I did was adding the frameworks to the embedded binaries for the three different targets. This happened to work very well but the watchKit extension doesn't seem to recognize the objects that I created within the iOS environment. How is it possible to have a shared Realm Database between those two devices?

解决方案

Update: Okay, thanks to chrisamanse's heads-up, I did some more research on this.

It turns out that App Groups are no longer possible on watchOS 2. Watch apps no longer run as extensions on the phone; they are now two isolated processes without any shared resources.

As such, this means it will be necessary to maintain a separate Realm file on both the watch and the phone and communicate any changes made to either through the WatchConnectivity framework.


Original: iOS applications and extensions (both Today widgets and watchOS apps) need to be considered as two wholly separate entities in their own separate containers. By default, an extension cannot access any files inside the container of its parent application. If you're saving a Realm file to the default path (i.e., the 'Documents' folder), there's no way for the watchOS app to access it from there.

Thankfully, it's possible to use the 'App Groups' feature of iOS to specify a shared folder that both the parent iOS app and the watchOS app can access, and can both read and write any Realm files that are in there.

Once you've enabled the App Groups entitlement in your app, it's just a matter of setting your Realm file's location to point to that location.

let sharedContainerURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.my.appgroups.bundleid")!
let realmURL = sharedContainerURL.appendingPathComponent("SharedRealm.realm")

let realmConfiguration = Realm.Configuration()
realmConfiguration.fileURL = realmURL
let realm = try! Realm(configuration: realmConfiguration)

There's a tutorial on the Realm website that explains how to make this work in more detail, however the API and Swift version are out of date at this point.

这篇关于与WatchOS共享领域数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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