Reload/refresh a scene after receive remote notification swiftUI

余生长醉 提交于 2020-02-06 08:44:40

问题


I have this problem. I'm receiving a notification from CloudKit using application:didReceiveRemoteNotification in AppDelegate. I'm able to receive the recordId, fetch it, and save it successfully. The problem is, the scene doesn't refresh.

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

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

in 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)
    }

In fetchAndSaveRemoteDataFromCloudKit, I'm passing the UserData in order to save the item received and it works. The problem is, I show the items on a List and the list doesn't refresh, even when I print the userData.items and the new item is there.

Is there a connection between AppDelegate and SceneDelegate in order to refresh the scene/window?

来源:https://stackoverflow.com/questions/59382727/reload-refresh-a-scene-after-receive-remote-notification-swiftui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!