dontWatchMyProfile
dontWatchMyProfile

Reputation: 46370

Can I use a keyPath in a predicate?

For some reason, this didn't work (although the keypath does exist):

The Entity is set on the Employee.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"department.departmentName == %@", departmentName];
[fetchRequest setPredicate:predicate];

NSError *fetchError = nil;
NSUInteger count = [moc countForFetchRequest:fetchRequest error:&fetchError]; // execution simply stops at this line, with no error or console log

Execution just stops at the last line above when asking for the count. I don't get an console log. Also I don't get any kind of exception. The execution just stops. There are no objects in the persistent store yet. So maybe it crashes because of it tries to follow a keypath in a nonexisting object? Does that make sense?

The line where GDB stops is this:

0x002a31cb  <+0459>  test   %eax,%eax

Previously to that, I see a lot of NSSQLAdapter... calls in the stack trace. There's definitely something wrong.

Well, but when I set the Entity to the destination of the key path and then just do something like

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"departmentName == %@", departmentName];

then there is no problem and count simply is 0.

Upvotes: 0

Views: 391

Answers (2)

TechZen
TechZen

Reputation: 64428

I've seen this "just stopping" behavior when you have a circular reference in your object graph. The fetch gets caught up in a loop. The compiler will usually warn you if this is the case but it doesn't stop you from running.

Upvotes: 2

diederikh
diederikh

Reputation: 25281

Make sure you also set the entity of the fetch request? with setEntity:

Upvotes: 1

Related Questions