热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

KVO与Swift中的共享NSUserDefaults-KVOwithsharedNSUserDefaultsinSwift

IamhavingissuescommunicatingbetweenmyhostappanditsextensionsthroughcommunicatingNSUser

I am having issues communicating between my host app and its extensions through communicating NSUserDefaults changes.

我通过传达NSUserDefaults更改在主机应用程序及其扩展之间进行通信时遇到问题。

I initialized NSUserDefaults using init(suiteName:), added KVO observer using the addObserver(...) method and overrided the method observeValueForKeyPath(...) but the method observeValueForKeyPath(...) is not called when I change the value corresponding to the observing key. It will be greatful if you help me to resolve this.

我使用init(suiteName :)初始化了NSUserDefaults,使用addObserver(...)方法添加了KVO观察者并覆盖了方法observeValueForKeyPath(...)但是当我更改了相应的值时没有调用observeValueForKeyPath(...)方法到观察的关键。如果你帮我解决这个问题会很棒。

PS:here suite name is App groups name and NSUserDefaults created using suiteName as group identifier will be inside the private area for app groups.

PS:此处套件名称是应用程序组名称,使用suiteName创建的NSUserDefaults作为组标识符将位于应用程序组的专用区域内。

1 个解决方案

#1


2  

The short answer is that you can't use KVO or even NSNotificationCenter on NSUserDefaults to communicate changes between an App Extension and the containing App.

简短的回答是,您无法在NSUserDefaults上使用KVO甚至NSNotificationCenter来在App扩展和包含应用程序之间进行通信。

There's a great post by Atomic Bird that looks at the ways of coordinating communication. In particular its interesting to look at his analysis of communicating user defaults changes:

Atomic Bird有一篇很棒的文章,着眼于协调沟通的方式。特别有趣的是,看看他对沟通用户默认值变化的分析:

A possible alternative for app/extension notifications is to use the Darwin notification center via CFNotificationCenterGetDarwinNotifyCenter, which actually is a little bit like NSDistributedNotificationCenter. There's some discussion of this by Wade Spires at Apple's dev forums site.

应用/扩展通知的另一种可能选择是通过CFNotificationCenterGetDarwinNotifyCenter使用Darwin通知中心,它实际上有点像NSDistributedNotificationCenter。在Apple的开发论坛网站上,Wade Spires对此进行了一些讨论。

I say "possible" because I'm not 100% confident of this continuing to work. In the documentation for this method, Apple notes that

我说“可能”,因为我对这种继续工作没有百分之百的信心。在这种方法的文档中,Apple注意到了这一点

An application has only one Darwin notification center, so this function returns the same value each time it is called.

应用程序只有一个Darwin通知中心,因此每次调用此函数都会返回相同的值。

So although this is apparently legal, it also sounds a lot like it violates the philosophy inherent in app extension restrictions, viz, that they can't access anything from the hosting app. This is why [UIApplication sharedApplication] is off limits in extensions. I can't help wonder if allowing CFNotificationCenterGetDarwinNotifyCenter is an oversight that might get "fixed" at some point.

因此,尽管这显然是合法的,但它听起来很像违反了应用扩展限制中固有的哲学,即他们无法从托管应用访问任何内容。这就是[UIApplication sharedApplication]在扩展中不受限制的原因。我不禁要问,是否允许CFNotificationCenterGetDarwinNotifyCenter是一种可能在某些时候“固定”的疏忽。

So I guess for now a good solution might be to use MMWormhole as they implement the above solution.

所以我想现在一个好的解决方案可能就是使用MMWormhole来实现上述解决方案。

Your other option is to use to check the user defaults every time your App becomes active and confirm whether any keys have changed, posting the relevant notifications etc.

您的另一个选择是每次您的应用程序处于活动状态时检查用户默认值,并确认是否有任何键已更改,发布相关通知等。

Good luck


推荐阅读
author-avatar
lenpre2017
林频仪器(http://www.linpin.com/)主营产品:盐雾试验箱、高低温试验箱、恒温恒湿试验箱,林频仪器是中国环境试验设备行业中唯一的股份制高新技术企业,是中国领先的环境试验设备制造商和可靠性环境试验解决方案综合服务商,拥有中国最大的环境试验设备生产基地,客户遍布全球40多个国家和地区。林频仪器现已发展成为环境试验设备领域的龙头企业。 ■ 凭借创新的产品、高效的供应链和强大的战略执行力,林频仪器锐意为全球用户打造优质、安全、环保的产品及专业高效的服务。 ■ 林频仪器产品系列包括:温
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有