5
- (void)fetchResult
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Project" ascending:YES];

    [request setEntity:self.entityDescription];
    [request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    self.fetchResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];

    NSError *fetchError = nil;
    BOOL success = [self.fetchResultController performFetch:&fetchError];
    if (success) {
        NSLog(@"fetched!");
    }
    else {
    NSLog(@"fetch fail!");
    }
}

The entityDescription and the context is set in the viewDidLoad method, as it is shown below:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;
self.entityDescription = [NSEntityDescription entityForName:@"Project" inManagedObjectContext:self.managedObjectContext];

Crash info: * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'keypath Project not found in entity '


Could anybody give a hand?;)

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
JackieLam
  • 668
  • 1
  • 7
  • 20

5 Answers5

13

The key used in the sort descriptor

[NSSortDescriptor sortDescriptorWithKey:@"Project" ascending:YES];

must be a property of your Core Data entity, not the name of the entity itself. So you should replace @"Project" in the sort descriptor by some property of the Project entity, such as @"name".

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
6

It is not related to the initial question, but it is highly related to the title of the question. And as it was the only one I've found for this topic, it may be useful for someone else:

So, I've got "Core Data crash: 'keypath Videos not found in entity ...'" because I've used string interpolation in a predicate:

let projectName = "Videos"
let predicate = NSPredicate(format: "\(#keyPath(Scene.relatedProject.name)) == \(projectName)")

The correct way is:

let projectName = "Videos"
let predicate = NSPredicate(format: "\(#keyPath(Scene.relatedProject.name)) == %@", projectName)

Hope, it will save someone hour(s) of figuring out what is going on.

ViktoR
  • 691
  • 9
  • 11
  • 1
    I believe adding single quotes around the string interpolation would also have been an acceptable solution. `let predicate = NSPredicate(format: "\(#keyPath(Scene.relatedProject.name)) == '\(projectName)'")` – ae14 Jun 10 '20 at 14:50
2

make sure you are using below syntax for your Query Example:

fetchRequest.predicate = NSPredicate(format: "username = %@", userName)

where username is the key and userName is variable to hold any name in the example.

Venu Gopal Tewari
  • 5,672
  • 42
  • 41
0

Set cache name like:

self.fetchResultController = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"temp"];
pkamb
  • 33,281
  • 23
  • 160
  • 191
Ajay Malviya
  • 131
  • 4
0
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

[fetchRequest setPredicate:predicate];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Project"          inManagedObjectContext:context];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"Column_Name"  ascending:YES];

[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

[fetchRequest setEntity:entity];

NSError *error = nil;
NSArray *arrTemp = [context executeFetchRequest:fetchRequest error:&error];

if([arrTemp count]>0){
   NSLog(@"%@",arrTemp);
}
Maddy B
  • 51
  • 1
  • 6