我对核心数据中的多对多关系都很陌生并且要了解它我已经创建了一个多对多关系的示例,如下图所示。
下面的代码显示了如何填充和检索数据。如果有人能告诉我这是否是实现多对多关系的正确方法,我将非常感激。
// First Course object
Course *first = (Course *) [NSEntityDescription
insertNewObjectForEntityForName:@"Course"
inManagedObjectContext:[self managedObjectContext]];
first.title = @"Core Data for iOS and OS X";
first.releaseDate = [dateFormatter dateFromString:@"16 Oct 2012"];
// Second Course object
Course *second = (Course *) [NSEntityDescription
insertNewObjectForEntityForName:@"Course"
inManagedObjectContext:[self managedObjectContext]];
second.title = @"C/C++ Essential Training";
second.releaseDate = [dateFormatter dateFromString:@"26 Jun 2012"];
// Third Course object
Course *third = (Course *) [NSEntityDescription
insertNewObjectForEntityForName:@"Course"
inManagedObjectContext:[self managedObjectContext]];
third.title = @"Java Essential Training";
third.releaseDate = [dateFormatter dateFromString:@"14 December 2011"];
// Fourth Course object
Course *fourth = (Course *) [NSEntityDescription
insertNewObjectForEntityForName:@"Course"
inManagedObjectContext:[self managedObjectContext]];
fourth.title = @"iOS SDK: Building Apps with MapKit and Core Location";
fourth.releaseDate = [dateFormatter dateFromString:@"3 August 2012"];
// Fifth Course object
Course *fifth = (Course *) [NSEntityDescription
insertNewObjectForEntityForName:@"Course"
inManagedObjectContext:[self managedObjectContext]];
fifth.title = @"Cocoa Essential Training";
fifth.releaseDate = [dateFormatter dateFromString:@"1 August 2012"];
// First Lecturer object
Lecturer *author = (Lecturer *) [NSEntityDescription
insertNewObjectForEntityForName:@"Lecturer"
inManagedObjectContext:[self managedObjectContext]];
author.name = @"Smith";
[author addCoursesObject:first];
[author addCoursesObject:second];
[author addCoursesObject:third];
// Second Lecturer object
Lecturer *author2 = (Lecturer *) [NSEntityDescription
insertNewObjectForEntityForName:@"Lecturer"
inManagedObjectContext:[self managedObjectContext]];
author2.name = @"John";
[author2 addCoursesObject:first];
[author2 addCoursesObject:third];
[author2 addCoursesObject:fourth];
[author2 addCoursesObject:fifth];
这就是我如何获取特定讲师所教授的所有课程。
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SUBQUERY(lecturers, $c, $c.name == 'John').@count > 0"];
[fetchRequest setPredicate:predicate];
NSError *error = nil;
NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects == nil)
{
NSLog(@"Problem! %@",error);
}
NSLog(@"fetch object count %d", [fetchedObjects count]);
for (Course *c in fetchedObjects)
{
NSLog(@" %@", c.title);
}
提前致谢。
答案 0 :(得分:2)
您的代码看起来正确,但是谓词
[NSPredicate predicateWithFormat:@"SUBQUERY(lecturers, $c, $c.name == 'John').@count > 0"]
太复杂了。你这里不需要SUBQUERY。要获取所有课程 与具有给定名称的讲师有关,您可以使用
[NSPredicate predicateWithFormat:@"ANY lectures.name == 'John'"]
或更好
[NSPredicate predicateWithFormat:@"ANY lectures.name == %@", @"John"]
因为即使名称包含任何特殊字符,这也有效 如引号。
如果您需要中间表,请提出您的问题:
使用中间表的一个原因
Course <-->>CourseLecturer<<-> Lecturer
通过向中间实体添加排序键,您可以按指定顺序保留讲师(以及课程讲师)的课程。