views:

150

answers:

2

I successfully stored and retrieved a reference to an NSManagedObject using the example found in this site http://cocoawithlove.com/2008/08/safely-fetching-nsmanagedobject-by-uri.html

The problem is, the app crash whene triying to retrieve an NSManagedObject which has been deleted.

I tried the method isFault on the object, but it always returns no, even if the object IS there.

Here is my code I use to retrieve it:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSData *myData = [prefs objectForKey:@"activatedObject"];
    if (myData)
    {
        NSURL *myURL = [NSKeyedUnarchiver unarchiveObjectWithData:myData];
        NSManagedObjectID *myID = [self.persistentStoreCoordinator managedObjectIDForURIRepresentation:myURL];
        id myObject = [self.managedObjectContext objectWithID:myID];
        self.Object = myObject;

    } 
A: 

The docs mention that the object must be saved to the store before getting the objectID. Are you getting it before saving the store?

Also, just consider saving the value of a unique property of the the object and just perform a search instead.

Corey Floyd
Yes, it's saved. I retrieve it just after lunching the app.It would be complicated to set a unique property and then search it... But I'll try if there's any other solution.
Davide
It is only slightly more complicated. You shouldn't have to create a unique property. Hopefully there is a unique property for each entity already. Creating a fetch request based on something like a "name" property is pretty trivial.
Corey Floyd
So did you find the solution already?
dontWatchMyProfile
A: 

You can try call this method:

NSError *error = nil;
id myObject = [self.managedObjectContext existingObjectWithID:myID error:&error];

If object specified by myID cannot be fetched, or does not exist, or cannot be faulted, it returns nil.

jamapag
This didn't work for me.
Heath Borders