也许是这样的?使用 1:n 关系并不复杂,使用 n:m 会更有趣:P
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Course" inManagedObjectContext:context]];
NSPredicate* = [NSPredicate predicateWithFormat:@"startWeekTheCourseInTheTerm <= %d AND lastWeekTheCourseInTheTerm >= %d AND timeAndPlaces.weekday == %d", currentWeek, currentWeek, weekday];
[fetchRequest setPredicate:predicate];
或者如果您需要完整的一周(例如在 tableView 中显示)
NSPredicate* = [NSPredicate predicateWithFormat:@"startWeekTheCourseInTheTerm <= %d AND lastWeekTheCourseInTheTerm >= %d", currentWeek, currentWeek];
[fetchRequest setPredicate:predicate];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:@"timeAndPlaces.weekday" ascending:YES]]];
按工作日和 courseOrder 排序:(
所以你有
-> 星期一
--> 1
--> 2
--> 3
-> 星期二
--> 1
...
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:[[NSSortDescriptor alloc] initWithKey:@"timeAndPlaces.weekday" ascending:YES], [[NSSortDescriptor alloc] initWithKey:@"timeAndPlaces.courseOrder" ascending:YES], nil]];
最后一步:获取
NSError* err = nil;
list = [NSMutableArray arrayWithArray:[context executeFetchRequest:fetchRequest error:&err]];
[fetchRequest release];