繁体   English   中英

核心数据和用户默认值不存储数据

[英]Core Data and Userdefaults not storing data

当我的应用程序关闭时,Core Data 和 UserDefaults 不存储数据。

当我的应用程序关闭时我会收到通知,所以我将它存储在核心数据中并默认做一些事情。

当应用程序打开时,它会存储所有信息,但只有应用程序关闭,它不起作用,任何想法都可以这样做。

我将数据存储在以下函数中。

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
            let response = notification.request.content.userInfo["aps"] as! NSDictionary
             //here i will send response will store to core date
            self.defaults.set("1", forKey: "notifyid")
            completionHandler([.alert, .badge, .sound])
        }
    } 

您将其存储在错误的功能中。 仅当应用程序处于前台时才会调用 Will Present。 所以当应用程序在后台时它不会工作,而不是将呈现功能使用确实接收远程功能:

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
    let response = notification.request.content.userInfo["aps"] as! NSDictionary
    self.defaults.set("1", forKey: "notifyid")    
}

注意:请确保在后台模式下的应用程序功能中,您的远程通知和后台获取。 否则它不会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM