0

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.

utahwithak
  • 6,235
  • 2
  • 40
  • 62

2 Answers2

4

UILocalNotification is not serializable (it doesn't implement NSCoding), 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' userInfo property. If you need to delete a specific notification, you search your UIApplications' scheduledLocalNotifications array for the local notification with the corresponding managed object ID in its userInfo property and then cancel that one via cancelLocalNotification:.

MrMage
  • 7,282
  • 2
  • 41
  • 71
0

Heads up - I've been reviewing this topic, and I'm seeing some conflicting information with the answer from @MrMage :

  1. UILocalNotification does conform to NSCoding per the Apple doc
  2. Here's an answer on stackoverflow that describes how to store the local notification in nsdata which should enable you to store the localnotification as a property in an entity: Delete a particular local notification

Not an expert on this topic though, so any feedback would be great.

Community
  • 1
  • 1
kris
  • 2,516
  • 25
  • 29