ioscore-datansmanagedobjectnsmanagedobjectid

How can I get the NSManagedObjectID of an object directly after saving?


How can I get the NSManagedObjectID of an object directly after saving?

I've tried using the NSNotification NSManagedObjectContextDidSaveNotification and getting the updated/inserted values and getting the object id from one (the only) managed object, but it's giving me "Unrecognized Selector" when I try to grab the object id.

Can I even get the Object Id right after I save?

- (void)handleDidSaveNotification:(NSNotification *)note
{

    NSDictionary *dict = [note userInfo];
    NSDictionary *updatedDict = [dict valueForKey:@"updated"];

    NSLog(@"Notification: %@", dict);
    NSLog(@"Updated Info: %@", updatedDict);

    NSManagedObject *core = [updatedDict valueForKey:@"entity"];

    NSManagedObjectID *objectId = [core objectID];
}

Solution

  • You are trying to set a dictionary (updatedDict) when the returned data is a NSSet.
    you might simply need to get it from the set collection it is in ...

    NSSet* s = [dict valueForKey:@"updated"];
    [s valueForKey:@"objectID"]
    

    This will return a set of NSManagedObjectIDs.
    See NSSet on how to access objects.