Reputation: 5444
The code works fine in iOS5+:
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"FooBar"];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"ident"
ascending:NO
selector:@selector(compare:)]];
request.predicate = [NSPredicate predicateWithFormat:@"xxx = %@", @"yyy"];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:model.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
But in iOS 4, I got the following error:
'NSObjectInaccessibleException', reason: 'This fetch request (0x70d4700)
was created with a string name (FooBar), and cannot respond to -entity
until used by an NSManagedObjectContext'
2012-11-08 12:12:18.093 hello[1566:11d03] *** Terminating app due to uncaught
exception 'NSObjectInaccessibleException', reason: 'This fetch request
(0x70d4700) was created with a string name (FooBar), and cannot respond
to -entity until used by an NSManagedObjectContext'
*** Call stack at first throw:
(
0 CoreFoundation 0x012405a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x0104a313 objc_exception_throw + 44
2 CoreData 0x000be68f -[NSFetchRequest entity] + 159
3 CoreData 0x0019f7fb -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:] + 763
...
How can I resolve this error?
Upvotes: 1
Views: 601
Reputation: 50089
the problem is that the NSFetchRequest has no context to get the object from... you always need a specific context to get an entityDescription from. In ios5 the getting of entityDescription is nicely hidden and delayed till you execute the fetch :) In for its not
the header is wrong btw. it says its all good for ios4:
+ (NSFetchRequest*)fetchRequestWithEntityName:(NSString*)entityName NS_AVAILABLE(10_7, 4_0);
Id file a bug. Anyways@Martin R is correct :)
Upvotes: 0
Reputation: 539685
fetchRequestWithEntityName:
is available only in iOS 5 and later.
On iOS 4, you have to create a NSEntityDescription
first:
NSEntityDescription *fooBarEntity = [NSEntityDescription entityForName:@"FooBar" inManagedObjectContext:model.managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:fooBarEntity];
Upvotes: 7