Reputation: 2557
I've got a problem in the formulation of my query; this is the scenario:
based on this db schema, I know the naming part sucks a little, but this is a project which has been already kicked off, so I've to stick with that.
Now, my goal is to select all the characters with a determined pack_id and a certain category_id, i.e. all the characters from pack 1 in category 5, so this's my NSPredicate
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (charRelationship.pack_id == %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];
[request setEntity:[NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.managedObjectContext]];
[request setPredicate:predicate];
NSError *error;
NSArray *result = [self.managedObjectContext executeFetchRequest:request error:&error];
as soon as the compiler tries to execute the fetchRequest, it crashes and goes SIGABRT. I really hate the fact that xcode is not even giving me a clue about the exception so that I could figure it out myself. So after blindly trying to fix it with no success, I wonder if there's anybody out there who could help me. I've already red a ton of other threads on SO and elsewhere, but I couldn't find any solution.
thanks a lot
-k-
Upvotes: 0
Views: 258
Reputation: 550
Actually you must not store any id separately for an entity that is associated. E.g.: in your case you could just refer to category from the characters entity like:
[NSPredicate predicateWithFormat:@"catRel = %@", cat];
However, I'm not sure what are you going to do with the snippet. Am I right that you have a Category entity stored in your 'cat' variable, and just going to select it from the database with the predicate? I would say, that a category_id already identifies your category... whatever, try not user "charRelationship.pasck_id == %@" but load the corresponding Character entity and use "charRelationship = %@" where you just put your entity.
Upvotes: 0
Reputation: 1152
try using ANY for the relationship:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(category_id == %@) AND (ANY charRelationship.pack_id like %@)", [[cat valueForKey:@"category_id"] stringValue], curPack];
Upvotes: 1