3

我有这个问题。我正在使用 AppDelegate 中的 application:didReceiveRemoteNotification 接收来自 CloudKit 的通知。我能够接收到recordId,获取它并成功保存它。问题是,场景没有刷新。

final class UserData: ObservableObject {
@Published var items: [Item] = []

func saveFetchedItem(recordId: CKRecord.ID, reason: CKQueryNotification.Reason) {
    fetchAndSaveRemoteDataFromCloudKit(recordId: recordId, reason: reason, userData: self)
  }
}

在 AppDelegate 中:

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
   let notification: CKNotification = CKNotification(fromRemoteNotificationDictionary: userInfo)!

   if notification.notificationType == .query {
      let queryNotification = notification as! CKQueryNotification
      let recordId = queryNotification.recordID                

       let reason: CKQueryNotification.Reason = queryNotification.queryNotificationReason
        /// reasons:
        /// .recordCreated, .recordUpdated, .recordDeleted
        UserData().saveFetchedItem(recordId: recordId!, reason: reason)

        }
        completionHandler(.newData)
    }

在 fetchAndSaveRemoteDataFromCloudKit 中,我传递了 UserData 以保存收到的项目并且它可以工作。问题是,我在列表上显示项目并且列表不会刷新,即使我打印 userData.items 并且新项目在那里。

AppDelegate 和 SceneDelegate 之间是否存在连接以刷新场景/窗口?

4

0 回答 0