菜鸟教程小白 发表于 2022-12-11 18:04:40

ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用


                                            <p><p>首先请注意,我没有像其他各种问题中所述那样多次注册为观察员。</p>

<p>当我在 native 日历应用程序中更改某些内容并返回到我的应用程序时,选择器会被调用 4-5 次并发出不同的通知。</p>

<p>在 <code>viewDidLoad</code> 中,我删除所有可能的观察者并注册一次 agian:</p>

<pre><code>NotificationCenter.default.removeObserver(self)
NotificationCenter.default.addObserver(self, selector: #selector(reloadModelData(notification:)), name: Notification.Name.EKEventStoreChanged, object: nil)
</code></pre>

<p>对应的方法:</p>

<pre><code>@objc private func reloadModelData(notification: NSNotification) {
    debugPrint(&#34;method called \(notification)&#34;)
}
</code></pre>

<p>导致这个输出</p>

<blockquote>
<p>&#34;method called NSConcreteNotification 0x170246300 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n    EKEventStoreChangedObjectIDsUserInfoKey =   (\n      \&#34;x-apple-eventkit:///Location/p259707\&#34;,\n      \&#34;x-apple-eventkit:///Event/p264955\&#34;\n    );\n}}&#34;
&#34;method called NSConcreteNotification 0x174258840 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n    EKEventStoreChangedObjectIDsUserInfoKey =   (\n      \&#34;x-apple-eventkit:///Location/p259707\&#34;,\n      \&#34;x-apple-eventkit:///Event/p264955\&#34;\n    );\n}}&#34;
&#34;method called NSConcreteNotification 0x17024b250 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n    EKEventStoreChangedObjectIDsUserInfoKey =   (\n      \&#34;x-apple-eventkit:///Location/p259707\&#34;,\n      \&#34;x-apple-eventkit:///Event/p264955\&#34;\n    );\n}}&#34;
&#34;method called NSConcreteNotification 0x174253b00 {name = EKEventStoreChangedNotification; object = ; userInfo = {\n    EKEventStoreChangedObjectIDsUserInfoKey =   (\n      \&#34;x-apple-eventkit:///Location/p259707\&#34;,\n      \&#34;x-apple-eventkit:///Event/p264955\&#34;\n    );\n}}&#34;</p>
</blockquote>

<p>有谁知道如何解决这个问题。意思是在重新进入应用时只收到该观察者的一次调用?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>我猜,这就是它的工作原理,日历会向您发送有关您所做的所有原始更改的通知。</p>

<p> <a href="https://developer.apple.com/reference/foundation/nsnotification.name/1507525-ekeventstorechanged" rel="noreferrer noopener nofollow">The Apple&#39;s documentation</a>建议通过调用 <code>refresh</code> 检查您正在访问的提醒和事件。如果它返回 <code>true</code>,则无需重新获取它们。所以多个通知到达不会造成太大问题。</p>

<p>我还建议在 <code>viewWillAppear(_:)</code> 中订阅通知并在 <code>viewDidDisappear(_:)</code> 中取消订阅。当然可能会有异常(exception),但通常你不想在屏幕不活动时处理它们。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/40613478/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/40613478/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 带有 EKEventStoreChanged 通知的 NotificationCenter 使用不同的通知多次调用