0

I'm a noob of Core Data. I have two entities Venue and Concert and two relationships concerts (from Venue to Concert) and venues (From Concerts to Venue). What I want to do is basically retrieve all the concerts for a venue that are contained between two dates (startDate-endDate).

NSPredicate *concertsToShow = [NSPredicate predicateWithFormat:@"ALL concerts.startDate <= %@ AND concerts.endDate >= %@",startDate,endDate];

    NSManagedObjectContext *managedObjectContext = ((AppDelegate *)[[UIApplication sharedApplication]delegate]).managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    [request setEntity:[NSEntityDescription entityForName:@"Venue" inManagedObjectContext:managedObjectContext]];
    [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"concerts", nil]];
    [request setPredicate:concertsToShow];

    NSError *error = nil;
    NSArray *concerts = [managedObjectContext executeFetchRequest:request error:&error];

    return concerts; 

When the code is run I got the following exception:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unsupported predicate (null)'*

I tried to put also parenthesis on the predicate but then it says that it cannot parse the predicate.

I ended up using a subquery

NSPredicate *concertsToShow = [NSPredicate predicateWithFormat:@"SUBQUERY(concerts, $i, $i.startDate BETWEEN {%@,%@}).@count>0",startDate,endDate];

The problem is that it performs a filtering only on the venues: it retrieves only the venues that have a concert which date is contained between startDate and endDate but the concerts associated are not filtered. What I want to achieve is to have this filtering PLUS a filtering on the concerts. So for example I have two Venues with Concerts associated (Only date shown in the example): "Irish Pub" ->> (01-04-2013, 02-04-2013,03-04-2013,04-04-2013) "English Pub" ->> (05-04-2013)

If the startDate and endDate of my predicate are respectively 02-04-2013 and 04-04-2013 The result should be:

"Irish Pub" ->> (02-04-2013,03-04-2013,04-04-2013)

2
  • 2
    Well, your problem is clear: the predicate is null?? I don't think you have to go to this much trouble, you should be able to make a predicate that adds your constraints easily..
    – Rob
    Apr 2, 2013 at 3:11
  • Any Idea why the predicate is null??
    – ubiAle
    Apr 2, 2013 at 3:16

1 Answer 1

0

As Rob mentioned, your error message says you have nil predicate.

so put the log and check predicated value before making Request.

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ALL concerts.startDate <= %@ AND concerts.endDate >= %@",startDate,endDate];
NSLog(@"my predicate is :%@",predicate);

also for good practices I would suggest,always do this way

if(predicate!=nil){
  //now make request here
}

let me know if you need more help in this.

1
  • 1
    I would add a logging breakpoint and simply put in: po predicate. But yes, that's the idea!
    – Rob
    Apr 2, 2013 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.