Question

I am working on local notifications and in order to identify each notification it needs to get a NSDictionary with the ManagedObjectID from the Core Data model as key of that dictionary.

The problem is thatwhen I do this I get the following error:

Property list invalid for format: 200 (property list dictionaries may only have keys which are CFStrings, not 'CFType')

which has to do with the fact that dictionaries can only store certain data types and not NSManagedObjectID.

So my question is, is there a way to convert this NSManagedObjectId to a NSString or NSNumber for example. I really need that specific id in order to identify it later on to remove that specific notification.

Code thus far:

    -(void) scheduleNotificationAtDate:(NSDate*)date WithBody:(NSString*) body WithBillEventId:(NSManagedObjectID*) billEventId{

// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.alertBody = body;
localNotification.alertAction = @"Ga naar de Rekening Delen app";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;

//set the dictionary here,
NSDictionary *billEventIdentifier = [NSDictionary dictionaryWithObject:@"billevent_notification" forKey: billEventId];
localNotification.userInfo = billEventIdentifier;
NSLog(@"set local notification");
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Was it helpful?

Solution

The problem you are having is that for objects to be properly stored in a dictionary and then serialized to disk they must be NSCoding compliant which NSManagedObjectID is not.

Fortunately there is another way. There is a method on an NSManagedObjectID called URIRepresentation which will be you an identifier for a Core Data object and you can use that instead of storing the NSManagedObjectID. You can then turn that NSURL URIRepresentation back into an NSManagedObjectID like this example:

NSManagedObjectID *objectID = [context.persistentStoreCoordinator managedObjectIDForURIRepresentation:myObjectURI];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top