许多核心数据我的例子

时间:2015-01-24 05:58:58

标签: ios core-data

我对核心数据中的多对多关系都很陌生并且要了解它我已经创建了一个多对多关系的示例,如下图所示。

下面的代码显示了如何填充和检索数据。如果有人能告诉我这是否是实现多对多关系的正确方法,我将非常感激。

enter image description here

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

提前致谢。

1 个答案:

答案 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"]

因为即使名称包含任何特殊字符,这也有效 如引号。


如果您需要中间表,请提出您的问题:

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

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

    通过向中间实体添加排序键,您可以按指定顺序保留讲师(以及课程讲师)的课程。

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