Core Data 多表关联

1.概念简介

    coreData中存在复杂的数据关系时,一张表难以满足需求,此时就需要了解使用coreData多表的关联使用.

    如 课程表 和 章节表 的关系:一个课程跟章节的对应关系是 一对多,单一个章节只能对应一个课程; 而课程表 与 讲师表 之间的关系:一个课程可以有多个讲师,一个讲师也可以讲多个课程,他们之间是多对多的关系 .

    表之间就是靠这种相互约束的关系建立关联.

下面以电影表和演员表进行代码演示:

创建的步骤 :

1>创建Data Model;
2>创建Movie,Actor表,设置外键;
3>创建NSManagedObject subclass;
 
代码实现 :
  1 #import "ViewController.h"
  2 #import <CoreData/CoreData.h>
  3 #import "Actor.h"
  4 #import "Movie.h"
  5 
  6 
  7 @interface ViewController ()
  8 
  9 {
 10     NSManagedObjectContext *ctx;
 11    
 12 }
 13 @end
 14 
 15 @implementation ViewController
 16 
 17 - (void)viewDidLoad {
 18     [super viewDidLoad];
 19   
 20     //1.应用程序包的路径
 21     //    NSLog(@"%@",[[NSBundle mainBundle] resourcePath]);
 22    
 23     NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MovieInfo.momd" ofType:nil];
 24    
 25     //1.加载数据模型文件
 26     NSManagedObjectModel *manageModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:filePath]];
 27    
 28     //2.持久化数据存储调度器,指定存储的方式,负责应用与数据库之间的操作
 29     NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:manageModel];
 30    
 31     //3.指定数据库的路径
 32     NSString *dbfile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/CoreData.db"];
 33     NSLog(@"%@",dbfile);
 34    
 35     NSError *error = nil;
 36    
 37     //4.添加持久化存储的方式(可以是数据库,也可以是XML),并且会打开数据库
 38     [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[NSURL fileURLWithPath:dbfile] options:nil error:&error];
 39    
 40     if (error) {
 41        
 42         NSLog(@"打开数据库失败");
 43     }else{
 44         NSLog(@"打开数据库成功");
 45     }
 46    
 47     //4.存储数据,更新数据,查询数据  上下文
 48     ctx = [[NSManagedObjectContext alloc] init];
 49    
 50     [ctx setPersistentStoreCoordinator:coordinator];
 51    
 52     //5.添加电影数据
 53     [self insertMovieData];
 54    
 55     //6.查询电影数据
 56     [self queryMovieData];
 57    
 58 }
 59 
 60 //插入数据
 61 - (void)insertMovieData{
 62 
 63     //演员一
 64     Actor *actor1 = [NSEntityDescription insertNewObjectForEntityForName:@"Action" inManagedObjectContext:ctx];
 65     actor1.name = @"杰森斯坦森";
 66     actor1.age = @50;
 67     actor1.height = @1.55;
 68    
 69     //演员2
 70     Actor *actor2 = [NSEntityDescription insertNewObjectForEntityForName:@"Action" inManagedObjectContext:ctx];
 71     actor2.name = @"汤姆克鲁斯";
 72     actor2.age = @55;
 73     actor2.height = @1.60;
 74    
 75     //电影一
 76     Movie *m1 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:ctx];
 77     m1.title = @"绝命速递";
 78     m1.year = [NSDate date];
 79     m1.rating = @9.7;
 80     m1.actor = actor1;
 81    
 82     //电影二
 83     Movie *m2 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:ctx];
 84     m2.title = @"壮志凌云";
 85     m2.year = [NSDate date];
 86     m2.rating = @9.2;
 87     m2.actor = actor2;
 88    
 89     //电影三
 90     Movie *m3 = [NSEntityDescription insertNewObjectForEntityForName:@"Movie" inManagedObjectContext:ctx];
 91     m3.title = @"速7";
 92     m3.year = [NSDate date];
 93     m3.rating = @9.5;
 94     m3.actor = actor1;
 95    
 96     //将电影添加到上下文
 97     [ctx insertObject:m1];
 98     [ctx insertObject:m2];
 99     [ctx insertObject:m3];
100 
101    
102     //保存
103     BOOL isSuccess = [ctx save:nil];
104    
105     if (isSuccess) {
106         NSLog(@"插入数据成功!");
107     }
108 
109 }
110 
111 
112 //查询数据
113 - (void)queryMovieData{
114    
115     //查询斯坦森主演的电影
116     NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Movie"];
117    
118     //设置过滤条件
119     request.predicate = [NSPredicate predicateWithFormat:@"actor.name = '杰森斯坦森'"];
120    
121     //查询
122     NSArray *result = [ctx executeFetchRequest:request error:nil];
123     for (Movie *movie in result) {
124        
125          NSLog(@"电影标题 :%@  电影评分:%.2lf",movie.title,[movie.rating doubleValue]);
126     }
127 
128 
129 
130 }
原文地址:https://www.cnblogs.com/pengsi/p/4860166.html