I am storing some objects in core data that have inherent life spans. I was scheduling local notifications on object creation for this lifespan and then handling the object went the notification fired.
The problem is that the objects can end early. But the local notifications don’t know this and still fire at their respective time which leads to confusion. Is there a way to store a pointer to the notification in with the object? So if it ends early it can just cancel it.
I would like to stick with notifications as I need the user to know when it is finished, if it finishes normally. And notifications don’t depend on the app running.
Basic question can you store pointers to objects in core data. Second question is if local notifications change memory addresses during their lifespan
I know I could just cancel them all and reschedule the ones needed if one ends early but that just seems wasteful and hopefully there’s a better way.
UILocalNotificationis not serializable (it doesn’t implementNSCoding), so you can’t store it persistently with Core Data. I suggest you add the Core Data entities’ managedObjectID in serializable form (e.g. as an URL) to the notifications’userInfoproperty. If you need to delete a specific notification, you search yourUIApplications’scheduledLocalNotificationsarray for the local notification with the corresponding managed object ID in itsuserInfoproperty and then cancel that one viacancelLocalNotification:.