一对多关系iPhone - NSPredicate核心数据查询 [英] One to Many relationships iPhone - NSPredicate core data query
问题描述
我有一个日历,每个都有一些事件。我想找到日期> x的日历的所有活动。
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;
我向这样的日历添加了一个事件,但我没有线索关于为查询构造NSPredicate。任何帮助感谢
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;
}
我将关系名称更改为events,并尝试了以下操作:
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];
...
但是我收到了异常
由于未捕获异常NSUnknownKeyException而终止应用程序,原因:'[valueForUndefinedKey:]:此类不是关键值事件的键值编码。
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key Event.'
推荐答案
一些想法:
- 在您的模型中,
newRelationship
真的叫日历
? - 您要将
事件
重命名为事件
,以遵循命名约定如事件
和日历
是大写的,而属性如discipline
events
是小写的) - 将
日历
code> events 双向关系
- In your model, is
newRelationship
really calledcalendar
? - You want to rename
Events
toevents
to follow naming conventions (entities likeEvent
andCalendar
are capitalized, while attributes likediscipline
and relationships likeevents
are lowercase) - Wire up the
calendar
andevents
relationships in both directions
例如:
- (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];
}
如果日历名称足以唯一标识单个日历,这将工作。如果您有两个具有相同名称的日历,则必须添加其他搜索条件,例如 @(类似%@)...,calendar.category,...
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, ...
您还可以使用对象ID:
You can also use the object ID:
_predicateStr = [NSString stringWithFormat:@"(SELF == %@) AND (event.start > '%@') AND (event.finish < '%@')", calendar, startDate, endDate];
NSPredicate
适当命名为 NSPredicate编程指南
。
A good reference for NSPredicate
programming is Apple's aptly named NSPredicate Programming Guide
.
这篇关于一对多关系iPhone - NSPredicate核心数据查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!