views:

293

answers:

2

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?

A: 

This is a great article about storing and retrieving references to objects.

http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html

St3fan
Yeah, I followed that one. But it's talking about nsmanagedobject. I would like to know how to deal with subclasses of nsmanagedobject. Why it does say that they are incompatible types?
Davide
A: 

Regarding the warning, did you try to force type casting?

Tour *tempObject = (Tour *) [appDelegate.managedObjectContext objectWithID:myID];

The problem related to NSObjectInaccessibleException is solved in the link St3fan posted :)

PS: Remember that a subclass of nsmanagedobject is still a nsmanagedobject!

unixo