How to retrieve stored reference to an NSManagedObject subclass?

Posted by DavidDev on Stack Overflow See other posts from Stack Overflow or by DavidDev
Published on 2010-03-16T12:49:46Z Indexed on 2010/04/28 3:43 UTC
Read the original article Hit count: 320

Hi!

I have a NSManagedObject subclass named Tour. I stored the reference to it using this code:

    prefs = [NSUserDefaults standardUserDefaults];
NSURL *myURL = [[myTour objectID] URIRepresentation];
 NSData *uriData = [NSKeyedArchiver archivedDataWithRootObject:myURL];
 [prefs setObject:uriData forKey:@"tour"];

Now I want to retrieve it. I tried using:

NSData *myData = [prefs objectForKey:@"tour"];
  NSURL *myURL = [NSKeyedUnarchiver unarchiveObjectWithData:myData];

  TourAppDelegate *appDelegate = (TourAppDelegate *)[[UIApplication sharedApplication] delegate];

  NSManagedObjectID *myID = [appDelegate.persistentStoreCoordinator managedObjectIDForURIRepresentation:myURL];

  if (myID)
  {
  Tour *tempObject = [appDelegate.managedObjectContext objectWithID:myID]; //WARNING
  tour = tempObject;
  }

  if (tour) //instruction...

But it's giving me this warning "Incompatible Objective-c types. Initializing 'struct NSManagedObject *', expected 'struct Tour *'

Plus, when executing, it's giving me this: Terminating app due to uncaught exception 'NSObjectInaccessibleException', reason: 'CoreData could not fulfill a fault for '0x5001eb0

How can I solve this?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about nsmanagedobject