how to get the objectID from url?

3.8k views Asked by At

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 !

1

There are 1 answers

0
Gopal R On BEST ANSWER

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.