English 中文(简体)
One to Many relationships iPhone - NSPredicate core data query
原标题:

I have a have a number of calendars, which each have a number of events. I would like to find all the events for a calendar where date > x.

My method signature looks like this

-(NSArray*)eventsForCalender:(Calendar*)calender StartDate:(NSDate*)start endDate:(NSDate*)endDate;

I added an Event to a calender like this, but I don t have a clue how to go about to construct an NSPredicate for the query. Any help appreciated

-(Event*)newEventforCalender:(Calendar*)calender
{
    Event * newEvent = (Event*)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:app.managedObjectContext];

    NSMutableSet * eventsArray = [calender mutableSetValueForKey:@"Events"];
    [eventsArray addObject:newEvent];
    [app.managedObjectContext processPendingChanges];

    return newEvent;
}

alt text http://img4.imageshack.us/img4/5218/screenshot20091120at140.png

I changed the relationship name to "events" and tried the following

....
    NSEntityDescription * entity = [NSEntityDescription entityForName:@"Calendar" inManagedObjectContext:app.managedObjectContext];
    [request setEntity:entity]; 

    NSPredicate * predictate = nil;
    predictate = [NSPredicate predicateWithFormat:@"(name == %@) AND (Event.start >  %@ ) AND (Event.finish <  %@ )", calender.name, start, endDate, nil];
    [request setPredicate:predictate];
...

However i m getting an exception

Terminating app due to uncaught exception NSUnknownKeyException , reason: [ valueForUndefinedKey:]: this class is not key value coding-compliant for the key Event.

问题回答

Some thoughts:

  1. In your model, is newRelationship really called calendar?
  2. You want to rename Events to events to follow naming conventions (entities like Event and Calendar are capitalized, while attributes like discipline and relationships like events are lowercase)
  3. Wire up the calendar and events relationships in both directions

That said and done, you could use a predicate like:

- (NSArray *) eventsForCalendar:(Calendar *)calendar startDate:(NSDate *)startDate endDate:(NSDate *)endDate {

    NSPredicate *_predicate;
    NSString *_predicateStr;
    NSSet *_calendarQueryResults;

    _predicateStr = [NSString stringWithFormat:@"(name like  %@ ) AND (event.start >  %@ ) AND (event.finish <  %@ )", calendar.name, startDate, endDate];
    _predicate = [NSPredicate predicateWithFormat:_predicateStr];
    _calendarQueryResults = [_managedObjectContext fetchObjectsForEntityName:@"Calendar" withPredicate:_predicate];

    return [_calendarQueryResults allObjects];
}

This will work if the calendar name is sufficient for uniquely identifying an individual calendar. If you have two calendars with the same name, you would have to add an additional search criterium, like @"(category like %@ ) ... ", calendar.category, ...

You can also use the object ID:

_predicateStr = [NSString stringWithFormat:@"(SELF == %@) AND (event.start >  %@ ) AND (event.finish <  %@ )", calendar, startDate, endDate];

A good reference for NSPredicate programming is Apple s aptly named NSPredicate Programming Guide.





相关问题
Code sign Error

I have created a new iPhone application.I have two mach machines. I have created the certificate for running application in iPhone in one mac. Can I use the other mac for running the application in ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

将音频Clips从Peter改为服务器

我不禁要问,那里是否有任何实例表明从Peit向服务器发送音响。 I m不关心电话或SIP风格的解决办法,只是一个简单的袖珍流程......

• 如何将搜查线重新定位?

我正试图把图像放在搜索条左边。 但是,问题始于这里,搜索条线不能重新布署。

热门标签