Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the objectID from url?

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 !

like image 902
yjw44 Avatar asked May 30 '12 06:05

yjw44


1 Answers

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.

like image 118
Gopal R Avatar answered Oct 06 '22 01:10

Gopal R