了解手表应用程序何时收到来自 Cloudkit 的推送通知

问题描述 投票:0回答:1

我有一个带有 Apple Watch 扩展的应用程序。

两者都使用核心数据。

我已在两者上启用了 CloudKit 功能。

我已按照此处

所述配置了两个应用程序

我打开 iOS 应用程序,更改数据库,但手表上没有任何反应。我的意思是,结果不会在手表上更新。

两个问题:

  1. 我需要执行某些操作才能推送或接收来自 iCloud 的通知吗?
  2. 我如何知道手表何时收到来自 iCloud 的通知?这是调试的方法吗?
ios swift swiftui apple-watch cloudkit
1个回答
0
投票

我已经按照 Apple 在我在问题中提到的文档中所写的内容 100% 配置了我的 iOS 和 Watch 应用程序。显然,Apple 编写的所有内容都是垃圾,并且应用程序没有通过 CloudKit 同步。

解决方案是将

PersistenceController
修改为:

import CoreData

struct PersistenceController {
  static let shared = PersistenceController()
  let container: NSPersistentCloudKitContainer
  
  var context: NSManagedObjectContext {
    return container.viewContext
  }
  
  init(inMemory: Bool = false) {
    container = NSPersistentCloudKitContainer(name: "MyApp")
    container.viewContext.automaticallyMergesChangesFromParent = true
    container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy

    guard let description = container.persistentStoreDescriptions.first else{
      fatalError("###\(#function): Failed to retrieve a persistent store description.")
    }
    
    description.setOption(true as NSNumber, forKey: NSPersistentHistoryTrackingKey)
    
    // Generate NOTIFICATIONS on remote changes
    description.setOption(true as NSNumber, forKey: NSPersistentStoreRemoteChangeNotificationPostOptionKey)
    description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "iCloud.com.myCompany.MyApp")

    
    if inMemory {
      container.persistentStoreDescriptions.first!.url = URL(fileURLWithPath: "/dev/null")
    }

    container.loadPersistentStores(completionHandler: { (storeDescription, error) in
      if let error = error as NSError? {
        fatalError("Unresolved error \(error), \(error.userInfo)")
      }
    })
  }
}

然后从 iPhone 和 Watch 中删除应用程序并再次运行。它会立即起作用。

© www.soinside.com 2019 - 2024. All rights reserved.