博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用CoreData [2]
阅读量:6612 次
发布时间:2019-06-24

本文共 4333 字,大约阅读时间需要 14 分钟。

使用CoreData [2]

此篇讲解CoreData处理关系型数据.

 

1. 先创建出Student于Teacher的实体.

 

2. 确定关系,并修改描述

 

3. 创建对象,并查看一下关系(Teacher与Student之间是有着关联关系的哦)

 

4. 测试代码.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    NSLog(@"%@", NSHomeDirectory());        // 创建老师的实体    Teacher *KongZi = [self createTeacher:@"KongZi"];        // 创建学生的实体并添加到老师的实体当中    NSArray *students =  @[@{
@"name":@"王力宏", @"age":@11}, @{
@"name":@"张惠妹", @"age":@8}, @{
@"name":@"井冈山", @"age":@10}]; [students enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { Student *student = [self createStudent:obj[@"name"] age:obj[@"age"]]; [KongZi addStudentsObject:student]; }]; // 存储信息 [self saveContext]; return YES;}- (Student *)createStudent:(NSString *)name age:(NSNumber *)age{ // 实体描述信息 NSEntityDescription *description = \ [NSEntityDescription entityForName:@"Student" inManagedObjectContext:[self managedObjectContext]]; // 初始化对象 Student *student = [[Student alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]]; student.name = name; student.age = age; return student;}- (Teacher *)createTeacher:(NSString *)name{ // 实体描述信息 NSEntityDescription *description = \ [NSEntityDescription entityForName:@"Teacher" inManagedObjectContext:[self managedObjectContext]]; // 初始化对象 Teacher *teacher = [[Teacher alloc] initWithEntity:description insertIntoManagedObjectContext:[self managedObjectContext]]; teacher.name = name; return teacher;}

 

5. 验证关系型数据

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    NSLog(@"%@", NSHomeDirectory());        // 设定要查询的实体    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Teacher"];        // 取出查询结果    NSArray *teachers = [[self managedObjectContext] executeFetchRequest:fetch error:nil];        // 遍历出所有老师    [teachers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        Teacher *teacher = obj;                // 获取该老师所有的学生        [[teacher students] enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {            Student *student = obj;            NSLog(@"%@ %@", student.age, student.name);                        Teacher *tmp = (Teacher *)student.teacher;            NSLog(@"老师是: %@", tmp.name);        }];    }];        return YES;}

 

*6. 验证存在两个老师的情形

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 本人意淫一回当老师    Teacher *YouXianMing = [self createTeacher:@"游贤明"];        // 设定要查询的实体    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Teacher"];        // 取出查询结果    NSArray *teachers = [[self managedObjectContext] executeFetchRequest:fetch error:nil];        // 遍历出所有老师    [teachers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        Teacher *teacher = obj;                // 获取该老师所有的学生        [[teacher students] enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {            Student *student = obj;                        // 添加一个学生后就停止            [YouXianMing addStudentsObject:student];            *stop = YES;        }];    }];        // 存储信息    [self saveContext];        return YES;}

执行上述代码之后在执行下述代码.

下图表明了这个原因:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 设定要查询的实体    NSFetchRequest *fetch = [NSFetchRequest fetchRequestWithEntityName:@"Teacher"];        // 取出查询结果    NSArray *teachers = [[self managedObjectContext] executeFetchRequest:fetch error:nil];        // 遍历出所有老师    [teachers enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {        Teacher *teacher = obj;                // 获取该老师所有的学生        [[teacher students] enumerateObjectsUsingBlock:^(id obj, BOOL *stop) {            Student *student = obj;            Teacher *tmp = (Teacher *)student.teacher;                        NSLog(@"%@ %@", student.age, student.name);            NSLog(@"老师是: %@", tmp.name);        }];    }];        return YES;}

 

附录:

如果想修改多对多,请以下图为参考

上图那么修改了之后就会变成下图所示的多对多了,一个学生也可以有好几个老师了.

以上就讲完了关系型数据:)

 

转载地址:http://xrxso.baihongyu.com/

你可能感兴趣的文章
ovirt官方安装文档 附录G
查看>>
磁盘故障小案例
查看>>
了解相关.NET Framework不同组件区别及安装知识
查看>>
ToughRADIUS快速指南
查看>>
HTML
查看>>
【转】左手坐标系和右手坐标系
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
POJ 3335 Rotating Scoreboard 半平面交
查看>>
window.location.href和window.location.replace的区别
查看>>
《Gamestorming》读书笔记
查看>>
域名和网址链接被微信浏览器拦截怎么办 微信屏蔽网址打开如何解决
查看>>
使用SQL Server Analysis Services数据挖掘的关联规则实现商品推荐功能(二)
查看>>
ubuntu下安装jdk
查看>>
C/S与B/S架构比较
查看>>
XML学习总结(2)——XML简单介绍
查看>>
python操作数据库-安装
查看>>
vs.net删除转移文件
查看>>
你真的了解interface和内部类么
查看>>
java中常用的类型转换
查看>>