marsalal1014
marsalal1014

Reputation: 1907

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;
}

Upvotes: 1

Views: 9659

Answers (3)

John
John

Reputation: 1

You can also use setter method from CoraData ... Just do something like this...

On your CustomCoreDataManager.m

#import "ObjectiveRecord.h" 

call init method like this

-(instancetype)init {

self = [super init];

if (self) {

[[CoreDataManager sharedManager] setModelName:@"YourModelName"]; }

return self; }

Hope this helps to someone...

Upvotes: 0

deanWombourne
deanWombourne

Reputation: 38475

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.

Upvotes: 7

Dave DeLong
Dave DeLong

Reputation: 243146

Are you sure you spelled the entity name correctly? Are you sure the managed object context's managed object model has an entity with that name? Either of those things could cause +entityForName:inManagedObjectContext: to return nil, which is almost definitely your problem.

Upvotes: 1

Related Questions