我对 Core Data 中的多对多关系非常陌生,为了学习它,我创建了一个多对多关系示例,如下图所示。
下面的代码显示了如何填充和检索数据。如果有人能告诉我这是否是实现多对多关系的正确方法,我将非常感激。
// 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];
这就是我获取特定讲师教授的所有类(class)的方式。
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(@"roblem! %@",error);
}
NSLog(@"fetch object count %d", [fetchedObjects count]);
for (Course *c in fetchedObjects)
{
NSLog(@" %@", c.title);
}
提前致谢。
Best Answer-推荐答案 strong>
您的代码看起来正确,但谓词
[NSPredicate predicateWithFormat:@"SUBQUERY(lecturers, $c, $c.name == 'John').@count > 0"]
太复杂了。您在这里不需要 SUBQUERY。获取所有类(class)
与具有给定名称的讲师相关的,您可以使用
[NSPredicate predicateWithFormat:@"ANY lectures.name == 'John'"]
或更好
[NSPredicate predicateWithFormat:@"ANY lectures.name == %@", @"John"]
因为即使名称包含任何特殊字符,这也有效
比如引号。
如果您需要中间表,请回答您的问题:
关于ios - 多对多核心数据我的例子,我们在Stack Overflow上找到一个类似的问题:
https://stackoverflow.com/questions/28122785/
|