Reputation: 850
There are two entities:
I want to make a course table.
The first entity is Course
, the second is TimeAndPlace
. At university, there are some courses and one course maybe has different time or place. Now I want to fetch Course
s and their associated TimeAndPlace
s in every weekday. But there some limits: if current week is between start week and last week whose period in term, the course is needed; in this courses, select the course whose weekday equal to a certain weekday. Finally, I want to get the course and their associated time and place.
I'm not too familiar with Core Data, Can you help me?
Edited: Just like SQL:
SELECT Course.courseName, TimeAndPlace.courseLocation, TimeAndPlace.courseOrder
FROM Course, TimeAndPlace
WHERE Course.startWeekTheCourseInTheTerm <= currentWeek AND Course.lastWeekTheCourseInTheTerm >= currentWeek AND timeAndPlaces.weekday == currentweekday;
effect drawing:
Upvotes: 2
Views: 414
Reputation: 1791
maybe something like this? with an 1:n relation it's not that complicated, will get more fun with 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];
or if you need complete week (for example to show in a 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]]];
sort by weekday and courseOrder:
(so you have
-> Monday
--> 1
--> 2
--> 3
-> Tuesday
--> 1
...
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:[[NSSortDescriptor alloc] initWithKey:@"timeAndPlaces.weekday" ascending:YES], [[NSSortDescriptor alloc] initWithKey:@"timeAndPlaces.courseOrder" ascending:YES], nil]];
last step: fetching
NSError* err = nil;
list = [NSMutableArray arrayWithArray:[context executeFetchRequest:fetchRequest error:&err]];
[fetchRequest release];
Upvotes: 1