ios - 核心数据 : fetch result from two entities, 和结果包含两个实体中的信息

标签 ios core-data

有两个实体:

enter image description here

我想做一个类(class)表。

第一个实体是Course,第二个是TimeAndPlace。在大学里,有一些类(class),一门类(class)可能有不同的时间或地点。现在我想在每个工作日获取 Course 及其关联的 TimeAndPlace。但有一些限制:如果当前周在开学周和学期的最后一周之间,则需要该类(class);在本类(class)中,选择工作日等于某个工作日的类(class)。最后,我想获得类(class)及其相关的时间和地点。

我对 Core Data 不太熟悉,你能帮我吗?

已编辑: 就像 SQL 一样:

SELECT Course.courseName, TimeAndPlace.courseLocation, TimeAndPlace.courseOrder
FROM Course, TimeAndPlace
WHERE Course.startWeekTheCourseInTheTerm <= currentWeek AND Course.lastWeekTheCourseInTheTerm >= currentWeek AND timeAndPlaces.weekday == currentweekday;

效果图:

enter image description here

最佳答案

也许是这样的?有了 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]]];

按工作日和类(class)顺序排序:
(所以你有

->星期一
--> 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];

关于ios - 核心数据 : fetch result from two entities, 和结果包含两个实体中的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16714826/

相关文章:

iphone - 比较 Cell 的 indexpath 和 uiButton 这可能吗?

IOS 12 决定键盘类型,即使我通过代码更改它

iPhone核心数据: Initializing Managed Object without a context

objective-c - 当删除前一个元素时,如何设置与其他元素的一对一关系?

ios - NSManagedObject 子类的 SectionNameKeyPath 类别方法可以带参数吗?

ios - 如何计算属性中的记录数?

ios - 如何使用我的 ios 应用程序提供的图像打开 Instagram 应用程序?

ios - 根据 subview 以编程方式设置 View 宽度大小

ios - 在 Swift 中采用 FIRGeoPoint 到 Codable 协议(protocol)

ios - 神奇的记录 NSFetchedResultsController NSManagedObject 更改保存不工作