Search code examples
ioscore-datansfetchrequest

iOS: executeFetchRequest:error: A fetch request must have an entity


I developed an application targeted for iPhone and it uses CoreData. All is working ok when I run it on the simulator, but when I run it on the device I'm getting the following error:

"*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'"

I have specified and defined an entity in my code as follows:

NSFetchRequest *select = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"NewsStand" inManagedObjectContext:[CDHelper sharedCDHelper].managedObjectContext];
[select setEntity:entity];
NSError *error;
NSMutableArray *results = [[[CDHelper sharedCDHelper].managedObjectContext executeFetchRequest:select error:&error] mutableCopy];

The error occurs when I execute the fetch to show in a tableView what has stored the DB.

I also have defined a managedObjectModel:

- (NSManagedObjectModel *)managedObjectModel
 {
if (__managedObjectModel != nil)
{
    return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"newsStandModel" withExtension:@"momd"];
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    
return __managedObjectModel;
}

Solution

  • When you load the managed object model what do you specify as the model file name? (something .mom).

    The simulator is not case sensitive, the device is. i.e.

    if the file is called MyModel.mom then

    NSString *path = [NSBundle mainBundle] pathForResource:@"mymodel" type:@"mom"];
    NSURL *url = [NSURL fileURLWithString:string];
    myModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:url];
    

    works on the simulator but not on the device.