KVO与Swift中的共享NSUserDefaults [英] KVO with shared NSUserDefaults in Swift

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

问题描述

我通过传达NSUserDefaults更改来解决主机应用与其扩展之间的通信问题。

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

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

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.

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

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.

推荐答案

简短的回答是,你可以在 NSUserDefaults 上使用KVO甚至NSNotificationCenter来传递App Extension和包含应用程序之间的更改。

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.

这里有 Atomic Bird的精彩帖子,探讨协调沟通的方式。特别有趣的是看看他对通信用户默认值更改的分析:

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:


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

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.

我说可能,因为我对此持续100%有信心
工作。在此方法的文档中,Apple注意到

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

一个应用程序只有一个Darwin通知中心,所以这个
函数每次调用时返回相同的值。

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

所以虽然这显然是合法的,但听起来很像是因为
违反了app扩展限制中固有的哲学,即
他们无法从托管应用程序访问任何内容。这就是
[UIApplication sharedApplication]在扩展中不受限制的原因。我不能
帮助怀疑是否允许CFNotificationCenterGetDarwinNotifyCenter是
的监督,可能在某些时候固定。

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.

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

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

您的另一个选择是每次您的应用程序生效时检查用户默认值,并确认是否有任何密钥更改,发布相关通知等。

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.

祝你好运

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

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