Problems loading Core data


#1

I up to the first real test in chapter 16. I don’t have an errors in Xcode, but I get an exception in allItems. Looks like the fetchRequest comes up empty. Here’s the error:

Terminating app due to uncaught exception ‘Fetch Failed’, reason: ‘Reason: (null)’

Reason null is not very descriptive. If I remove the NSSortDescriptor I get the same thing.

I feel like I missed a step that would create the data base file. But it looks like everything is there. I have all of the classes and everything appears correct in Homepwner.xcdatamodeld.

There should not be any data yet since the app hasn’t created any records. So, in some respects results should come back empty.


#2

Poking around in the application folder I can see:

store.data
store.data-shm
store.data-wal

So it looks like the Core Data is creating something. I moved these to the desktop and tried rebuilding, which didn’t help.


#3

Looking closely at the debugger I noticed something strange. I set a break point at the top of this function. As I step through the code, when I get to the line NSError *error; The debugger shows:

err NSError * domain: <read memory from 0xd failed (0 of 4 bytes read)> - code: <read memory from 0x9 failed (0 of 4 bytes read)> 0x00000001

This looks strange, it should show something like error = nil. This should be declaring a local variable that hasn’t been assigned a value yet.

[code]-(void)loadAllItems {
if (!allItems) {
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *e = [[model entitiesByName] objectForKey:@“BNRItem”];
[request setEntity:e];

    /* */
    NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"orderingValue" ascending:YES];
    [request setSortDescriptors:[NSArray arrayWithObject:sd]];
    
    
    NSError *error; // *******
    
    NSArray *result = [context executeFetchRequest:request error:&error];
    if (!result) {
        [NSException raise:@"Fetch Failed" format:@"Reason: %@", [error localizedDescription]];
    }
    
    allItems = [[NSMutableArray alloc] initWithArray:result];
}

}
[/code]


#4

Problem solved, again it was a case of user error. Forgot to set the persistentStoreCoordinator.