简体   繁体   English

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

[英]Core Data and Userdefaults not storing data

Core Data and UserDefaults not storing data when my application is closed.当我的应用程序关闭时,Core Data 和 UserDefaults 不存储数据。

I am getting notification when my application is closed so i store it in core data and defaults to do some stuff.当我的应用程序关闭时我会收到通知,所以我将它存储在核心数据中并默认做一些事情。

when the application is open it is storing all informations but only application is closed its not working, any idea to do it.当应用程序打开时,它会存储所有信息,但只有应用程序关闭,它不起作用,任何想法都可以这样做。

I stored my data in the following function.我将数据存储在以下函数中。

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

You are storing it in wrong function.您将其存储在错误的功能中。 Will Present only gets called when the app is in Foreground.仅当应用程序处于前台时才会调用 Will Present。 so it will not work when app is in background, Instead of Will Present function use did receive remote function:所以当应用程序在后台时它不会工作,而不是将呈现功能使用确实接收远程功能:

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

Note: Please make sure in your app capabilities under Background Modes your remote notification and background fetch.注意:请确保在后台模式下的应用程序功能中,您的远程通知和后台获取。 Otherwise it won't work.否则它不会工作。

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

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