如何在 CoreData 中保存 UILocalNotifications
How Save UILocalNotifications in CoreData
答案在下面,图片在这里:
几天来我一直在搜索如何执行此操作,但只能找到在 NSUserDefaults 中存储 UILocalNotificaations 的人。将这些保存在 NSUserDefaults 中对我来说似乎是错误的,因为它应该用于小标志。我刚刚终于弄清楚如何在 CoreData 中存储通知。这是使用 Xcode 7.3.1 和 Swift 2.2
首先,您需要在 CoreDataModel 中创建一个新的 entity
然后向其添加一个属性。属性应该是类型 Binary Data 我将我的 table/entity 和我的属性命名为 "notification"。它应该看起来像这样:
上面问题中链接的图片。
接下来你需要给 UILocalNotification 添加一个扩展,它应该是这样的:
extension UILocalNotification {
func save() -> Bool {
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
let firedNotificationEntity = NSEntityDescription.insertNewObjectForEntityForName("ManagedFiredNotifications", inManagedObjectContext: appDelegate!.managedObjectContext)
guard appDelegate != nil else {
return false
}
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
firedNotificationEntity.setValue(data, forKey: "notification")
do {
try appDelegate!.managedObjectContext.save()
return true
} catch {
return false
}
}
}
现在要保存通知,您需要做的就是调用
UILocalNotification.save()
在您要保存的通知上。我的通知被命名为 'notification' 所以我会调用 notification.save()
要检索通知,您需要这样的方法
func getLocalFiredNotifications() -> [UILocalNotification]? {
let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)!.managedObjectContext
let firedNotificationFetchRequest = NSFetchRequest(entityName: "ManagedFiredNotifications")
firedNotificationFetchRequest.includesPendingChanges = false
do {
let fetchedFiredNotifications = try managedObjectContext.executeFetchRequest(firedNotificationFetchRequest)
guard fetchedFiredNotifications.count > 0 else {
return nil
}
var firedNotificationsToReturn = [UILocalNotification]()
for managedFiredNotification in fetchedFiredNotifications {
let notificationData = managedFiredNotification.valueForKey("notification") as! NSData
let notificationToAdd = NSKeyedUnarchiver.unarchiveObjectWithData(notificationData) as! UILocalNotification
firedNotificationsToReturn.append(notificationToAdd)
}
return firedNotificationsToReturn
} catch {
return nil
}
}
请注意,此 returns 一组 UILocalNotifications。
如果您打算删除其中的一些然后再次存储列表,那么在检索这些时,您应该在获得它们时删除它们,就像这样:
func loadFiredNotifications() {
let notifications = StudyHelper().getLocalFiredNotifications()
if notifications != nil {
firedNotifications = notifications!
} else {
// throw an error or log it
}
classThatRemoveMethodIsIn().removeFiredLocalNotifications()
}
我希望这可以帮助遇到与我在尝试实现此问题时遇到相同问题的人。
答案在下面,图片在这里:
几天来我一直在搜索如何执行此操作,但只能找到在 NSUserDefaults 中存储 UILocalNotificaations 的人。将这些保存在 NSUserDefaults 中对我来说似乎是错误的,因为它应该用于小标志。我刚刚终于弄清楚如何在 CoreData 中存储通知。这是使用 Xcode 7.3.1 和 Swift 2.2
首先,您需要在 CoreDataModel 中创建一个新的 entity 然后向其添加一个属性。属性应该是类型 Binary Data 我将我的 table/entity 和我的属性命名为 "notification"。它应该看起来像这样:
上面问题中链接的图片。
接下来你需要给 UILocalNotification 添加一个扩展,它应该是这样的:
extension UILocalNotification {
func save() -> Bool {
let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
let firedNotificationEntity = NSEntityDescription.insertNewObjectForEntityForName("ManagedFiredNotifications", inManagedObjectContext: appDelegate!.managedObjectContext)
guard appDelegate != nil else {
return false
}
let data = NSKeyedArchiver.archivedDataWithRootObject(self)
firedNotificationEntity.setValue(data, forKey: "notification")
do {
try appDelegate!.managedObjectContext.save()
return true
} catch {
return false
}
}
}
现在要保存通知,您需要做的就是调用
UILocalNotification.save()
在您要保存的通知上。我的通知被命名为 'notification' 所以我会调用 notification.save()
要检索通知,您需要这样的方法
func getLocalFiredNotifications() -> [UILocalNotification]? {
let managedObjectContext = (UIApplication.sharedApplication().delegate as? AppDelegate)!.managedObjectContext
let firedNotificationFetchRequest = NSFetchRequest(entityName: "ManagedFiredNotifications")
firedNotificationFetchRequest.includesPendingChanges = false
do {
let fetchedFiredNotifications = try managedObjectContext.executeFetchRequest(firedNotificationFetchRequest)
guard fetchedFiredNotifications.count > 0 else {
return nil
}
var firedNotificationsToReturn = [UILocalNotification]()
for managedFiredNotification in fetchedFiredNotifications {
let notificationData = managedFiredNotification.valueForKey("notification") as! NSData
let notificationToAdd = NSKeyedUnarchiver.unarchiveObjectWithData(notificationData) as! UILocalNotification
firedNotificationsToReturn.append(notificationToAdd)
}
return firedNotificationsToReturn
} catch {
return nil
}
}
请注意,此 returns 一组 UILocalNotifications。
如果您打算删除其中的一些然后再次存储列表,那么在检索这些时,您应该在获得它们时删除它们,就像这样:
func loadFiredNotifications() {
let notifications = StudyHelper().getLocalFiredNotifications()
if notifications != nil {
firedNotifications = notifications!
} else {
// throw an error or log it
}
classThatRemoveMethodIsIn().removeFiredLocalNotifications()
}
我希望这可以帮助遇到与我在尝试实现此问题时遇到相同问题的人。