簡體   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