gpt4 book ai didi

ios - 多对多核心数据我的例子

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:01:53 26 4
gpt4 key购买 nike

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

这就是我获取特定讲师教授的所有类(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(@"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。获取所有类(class)与具有给定名称的讲师相关,您可以使用

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

或更好

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

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


关于您是否需要中间表的问题:

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

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

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

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

关于ios - 多对多核心数据我的例子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28122785/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com