NSPersistentStoreRemoteChangeNotification没有被解雇 [英] NSPersistentStoreRemoteChangeNotification not getting fired

查看:219
本文介绍了NSPersistentStoreRemoteChangeNotification没有被解雇的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在使用 NSPersistentCloudKitContainer 的CoreData + CloudKit项目中执行历史记录跟踪。我一直在关注Apple的示例项目

I am trying to perform history tracking in my CoreData+CloudKit project which uses NSPersistentCloudKitContainer. I have been following along with Apple's sample project

我想在远程存储已更新时执行某些任务。对于此苹果,建议您在签名和签名中启用远程通知。功能的应用程序的后台模式部分。

I want to perform certain task when the remote store has been updated. For this apple recommends enabling remote notification in the Signing & capabilities's Background Mode section of the app.

我已为我的项目启用了历史记录跟踪功能,如Apple的示例项目所示。

I have enabled History Tracking for my project as shown in Apple's sample project.

    // turn on persistent history tracking
    let description = container.persistentStoreDescriptions.first
    description?.setOption(true as NSNumber,
                           forKey: NSPersistentHistoryTrackingKey)

    // ...

我也已经注册了我的商店

Also I have registered my store to listen for store changes.

    // turn on remote change notifications
    let remoteChangeKey = "NSPersistentStoreRemoteChangeNotificationOptionKey"
    description?.setOption(true as NSNumber,
                               forKey: remoteChangeKey)

    // ...

还添加了观察者以监听 NSPersistentStoreRemoteChangeNotification

Observer is also added to listen for NSPersistentStoreRemoteChangeNotification.

但是否 NSPersiste ntStoreRemoteChangeNotification 被解雇。为了确保我的实现没有错误,我只是将断点放在Apple提供的示例代码 @objc func storeRemoteChange(_ notification:Notification)中,但是我仍然可以

However there is no NSPersistentStoreRemoteChangeNotification being fired. To make sure there is no mistake in my implementation, I am have simply put breakpoints in @objc func storeRemoteChange(_ notification: Notification) the Apple's provided sample code but still I can not see any notification being fired and no breakpoints are activated.

我已经了解了示例项目中对Tag的重复数据删除,并尝试对其进行测试,但未成功。是Apple实施中的错误,还是我缺少所需的任何设置?

I have understood the deduplication of the Tags done in the sample project and also tried testing it but without any success. Is it a bug in the Apple's implementation or am I missing any setup which is required?

推荐答案

我的猜测是您正在观察容器而不是商店协调器,请像这样添加观察者:

My guess is you are observing the container instead of the store coordinator, add your observer like this:

    NotificationCenter.default.addObserver(
        self, selector: #selector(type(of: self).storeRemoteChange(_:)),
        name: .NSPersistentStoreRemoteChange, object: container.persistentStoreCoordinator)

请注意最后一个参数 container.persistentStoreCoordinator

,此通知会出现在所有不同的线程上,因此请小心并发。只需在该方法中休眠5秒钟,您就会在应用启动时看到3个不同的线程调用它。这很可能就是为什么在示例中有一个 historyQueue 带有 maxOperationCount 1来处理它的原因。

And a warning, this notification comes in on all different threads so you be careful with concurrency. Just put a 5 second sleep in the method and you'll see on app launch 3 different threads call it. This is likely why in the example there is a historyQueue with maxOperationCount 1 to handle it.

某些通知在 userInfo 中不确定 NSPersistentHistoryTokenKey 为什么。

Some notifications have NSPersistentHistoryTokenKey in the userInfo not sure why.

这篇关于NSPersistentStoreRemoteChangeNotification没有被解雇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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