how to get the objectID from url?
Asked Answered
F

1

5

my code is below :

NSURL *urlID = [objID URIRepresentation];
    NSString *strID = [urlID absoluteString];
    NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:strID, @"objectID", nil];
    localNotification.userInfo = infoDict;

then i want to get the objected like this:

NSString *strID = [notification.userInfo objectForKey:@"objectID"];
NSURL *urlID = [[NSURL alloc] initWithString:strID];
NSManagedObjectID *objID = [[NSPersistentStoreCoordinator alloc] managedObjectIDForURIRepresentation:urlID];

but the objID is nil. anything wrong ? how to do that ? thank you !

Falgoust answered 30/5, 2012 at 6:49 Comment(1)
Could it be that you are storing a newly created object ID before save and trying to retrieve it after save? Since object has a temporary ID at first and will be granted a permanent one during save, that would explain your issue.Hb
C
11

If this is your actual code, then the line below is suspect

NSManagedObjectID *objID = [[NSPersistentStoreCoordinator alloc] managedObjectIDForURIRepresentation:urlID];

You are allocating a new NSPersistentStoreCoordinator, uninitialized, without any stores added to it. As per the documentation, it will return nil if no matching stores are found.

If you have the managed object context handy, following should work

NSManagedObjectID *objId = [[[self managedObjectContext] persistentStoreCoordinator] managedObjectIDForURIRepresentation:urlID];

otherwise i'd agree with svena's reply.

Cosgrave answered 20/7, 2012 at 14:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.