Question

So i'm trying to work out how to use these predicates, i've read the Apple doc and am trying to use it (https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html) and i have the predicate set up, but it keep getting Thread 1: EXC_BAD_ACCESS (code =) etc.etc.

    NSError *error;
    NSLog(@"1");

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
   NSEntityDescription *entity = [NSEntityDescription entityForName:@"Fruit" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
  NSLog(@"2");
    NSPredicate *predicate = [NSPredicate predicateWithFormat:
                              @"Source.sourceName contains[cd] %@", "Apple Tree"];

    [fetchRequest setPredicate:predicate];
    NSLog(@"3");
    NSArray *fetchResult = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
    NSLog(@"4");
    testLbl.text = [fetchResult objectAtIndex:0];

Thats the code i'm using, as for the Core Data we have...

Entities Fruit & Source

Attributes fruitName & sourceName

Relationship (one to one) fruitSource<--------->sourceFruit

What i want to do is pull out any fruit that comes from an Apple Tree... >.<

Was it helpful?

Solution

There are two different problems:

  1. To get from Fruit to the related Source you have to use the relationship: @"fruitSource.sourceName contains ..." instead of @"Source.sourceName contains ...".

  2. (This is probably causing the exception.) The %@ format requires an Objective-C object as argument, not a C string: @"Apple Tree" instead of "Apple Tree".

So the predicate should look like this:

[NSPredicate predicateWithFormat:@"fruitSource.sourceName CONTAINS[cd] %@", @"Apple Tree"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top