一对多关系iPhone - NSPredicate核心数据查询 [英] One to Many relationships iPhone - NSPredicate core data query

查看:99
本文介绍了一对多关系iPhone - NSPredicate核心数据查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个日历,每个都有一些事件。我想找到日期> 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.'

推荐答案

一些想法:


  1. 在您的模型中, newRelationship 真的叫日历

  2. 您要将事件重命名为事件,以遵循命名约定如事件日历是大写的,而属性如 discipline events 是小写的)

  3. 日历 code> events 双向关系

  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

例如:

- (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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆