views:

39

answers:

1

-objectWithID: is supposed to give me an object that's broken when the ID doesn't exist. The documentation says, that this object throws an exception when I try to access an property.

However, it never throws any. Must I enable exceptions so that they're really thrown?

Here's some code:

// Assume: a new managed object has been created. Then it's ID has been converted to NSURL.
// The MO has been saved. Then the NSURL has been converted back to an NSManagedObjectID *tmpID
// So tmpID is an ID that doesn't exist anymore, since the ID of the MO has changed due to persisting it
@try {
    NSManagedObject *mo = [context objectWithID:tmpID]; // tmpID doesnt exist anymore!
    NSString *timeStamp = [[mo valueForKey:@"timeStamp"] description]; // nil
    [mo setValue:[NSDate date] forKey:@"timeStamp"];
}
@catch (NSException * e) {
    NSLog(@"Error: %@: %@", [e name], [e reason]); // never called
}
+1  A: 

How are you testing this? Core Data projects tend to have exceptions turned on by default.

What does your test look like?

Update

How do you know that tempID does not exist? Can you show the code that creates that tempID?

after saving, a tmpID turns into a permanent id, right? ...so when I convert it to NSURL to keep it around, and then convert it back after saving, it should be invalid...or not?

There is no guarantee of that. It would not surprise me if Core Data kept a lookup from the temp to the permanent for a while.

If you really want to see it throw an exception, change that temp to something else and throw that at Core Data. If you still don't get an exception after that then it is time to file a radar.

Marcus S. Zarra
all I had was simply an try-catch block with a piece of code that was supposed to throw an exception. objectWithID returned an MO from an ID that doesn't exist, and the MO was completely empty. I was able to access the properties and read out that they're 0x0 or nil, but no exception has been thrown at all. The documentation claims that it would happen, since -objectWithID: returns a "broken" object in case of bad ID. Accessing any property of this should throw an exception.
dontWatchMyProfile
updated with code example
dontWatchMyProfile
after saving, a tmpID turns into a permanent id, right? ...so when I convert it to NSURL to keep it around, and then convert it back after saving, it should be invalid...or not?
dontWatchMyProfile