I am not clear why I cannot save this to userprefs with:
/** save the queue to prefs so it can be processed at a later time */
func saveBackgroundNotificationsToPrefs(_ backgroundNotification:[Date:[App:NotificationSpecification]]) {
UserDefaults.standard.set(backgroundNotification, forKey: PreferencesModel.constants.backgroundNotificationsKey)
}
The NotificationSpecification object I am saving is:
internal var apps: [App] // this one saves in it's "saveAppsToPrefs" method
internal var backgroundNotifications: [Date:[App:NotificationSpecification]] // this one does not save.
my app struct is:
struct App: Hashable, Codable, Equatable {
let name: String
let notify: String
let id: String
let isSubscribed: Bool
}
and the notificationspecification is:
struct NotificationSpecification: Hashable, Codable {
let id: String // is also the app id
let p8: String
let key_id: String?
let team_id: String?
let topic: String
let custom_endpoint: String?
let custom_method: String?
let custom_headers: [String:String]?
init (id:String, p8:String, key_id:String, team_id:String, topic:String) {
self.id = id
self.p8 = p8
self.key_id = key_id
self.team_id = team_id
self.topic = topic
self.custom_endpoint = nil
self.custom_method = nil
self.custom_headers = nil
}
init (id:String, p8:String, topic:String, custom_endpoint:String, custom_method:String, custom_headers:[String:String]?) {
self.id = id
self.p8 = p8
self.topic = topic
self.custom_endpoint = custom_endpoint
self.custom_method = custom_method
self.custom_headers = custom_headers
self.key_id = nil
self.team_id = nil
}
}
The error that I am receiving is:
2019-04-27 12:58:54.812658-0400 Behavior-based Notifications[15130:4568207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object {
"" = {
"Behavior_based_Notifications.App(name: \"APP_NAME\", notify: \"{\\\"token\\\":\\\"APP_TOKEN\\\",\\\"body\\\":{\\\"to\\\":\\\"APP_TOKEN\\\",\\\"badge\\\":0,\\\"_category\\\":\\\"APP_CATEGORY\\\"}}\", id: \"APP_ID\", isSubscribed: true)" = "Behavior_based_Notifications.NotificationSpecification(id: \"SAME_APP_ID\", p8: \"custom\", key_id: nil, team_id: nil, topic: \"TOPIC\", custom_endpoint: Optional(\"HOST_URL"), custom_method: Optional(\"POST\"), custom_headers: Optional([\"host\": \"HOSTNAME\", \"content-type\": \"application/json\", \"accept-encoding\": \"gzip, deflate\", \"accept\": \"application/json\"]))";
};
} for key backgroundNotification'
is it because of custom_headers? what can I do about it -- do I have to manually transform it to/from json?
edit
Following Larme's suggestion:
/** save the queue to prefs so it can be processed at a later time */
func saveBackgroundNotificationsToPrefs(_ backgroundNotification:[Date:[App:NotificationSpecification]]) {
UserDefaults.standard.set(NSKeyedArchiver.archivedData(withRootObject: backgroundNotification), forKey: PreferencesModel.constants.backgroundNotificationsKey)
}
/** restore the queue from prefs so it can be processed at a later time */
func getBackgroundNotificationFromPrefs() -> [Date:[App:NotificationSpecification]] {
let defaultBackgroundNotification:[Date:[App:NotificationSpecification]] = [:]
guard let backgroundNotificationData = UserDefaults.standard.value(forKey:
PreferencesModel.constants.backgroundNotificationsKey) as? Data else {
saveBackgroundNotificationsToPrefs(defaultBackgroundNotification)
return defaultBackgroundNotification
}
guard let backgroundNotification = NSKeyedUnarchiver.unarchiveObject(with: backgroundNotificationData) as? [Date :
[App:NotificationSpecification]] else {
return defaultBackgroundNotification
}
return backgroundNotification
}
I am getting:
'archivedData(withRootObject:)' was deprecated in iOS 12.0: Use +archivedDataWithRootObject:requiringSecureCoding:error: instead
and
'unarchiveObject(with:)' was deprecated in iOS 12.0: Use +unarchivedObjectOfClass:fromData:error: instead
If I change it to NSKeyedArchiver.archivedDataWithRootObject( backgroundNotification)
it tells me that the function has been renamed to the form that is generating the warning.