多对多核心数据我的例子

Many to Many Core Data My Example

我对 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];

这就是我获取特定讲师教授的所有课程的方式。

  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);
        }

提前致谢。

您的代码看起来是正确的,但是谓词

 [NSPredicate predicateWithFormat:@"SUBQUERY(lecturers, $c, $c.name == 'John').@count > 0"]

太复杂了。您在这里不需要 SUBQUERY。获取所有课程 与具有给定名称的讲师相关,您可以使用

[NSPredicate predicateWithFormat:@"ANY lectures.name == 'John'"]

或更好

[NSPredicate predicateWithFormat:@"ANY lectures.name == %@", @"John"]

因为即使名称包含任何特殊字符,它也能正常工作 比如引号。


关于你是否需要中级 table 的问题:

  • 您的模型是有效的,如果它适合您,您应该使用它(实际上 Core Data 在内部创建了一个中间 SQLite table)。
  • 使用中间体的一个原因table

     Course <-->>CourseLecturer<<-> Lecturer
    

    是你可以通过在中间实体中添加排序键来保持讲师(和课程的讲师)的课程按指定顺序排列。

  • 中间实体的另一个原因是它使它成为可能 显示所有课程和讲师的 table,其中一个部分用于 每门课程。