CoreData表关联

来源:互联网 发布:个人网络招商 编辑:程序博客网 时间:2024/06/10 13:00

- (void) _initManyTable{


    //初始化模型

    NSManagedObjectModel * model=[NSManagedObjectModelmergedModelFromBundles:nil];

    

    //创建基础库协调器

    NSPersistentStoreCoordinator * psc=[[NSPersistentStoreCoordinatoralloc] initWithManagedObjectModel:model];

    

    //获取沙盒路径

    NSString * path=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)firstObject];

    path=[path stringByAppendingPathComponent:@"morestudent.sqlite"];

   NSLog(@"%@",path);

    

    //拼接URL

    NSURL * url=[[NSURLalloc] initFileURLWithPath:path];

    

    

    //为协调器添加基础库路径和基础库类型

    [psc addPersistentStoreWithType:NSSQLiteStoreTypeconfiguration:nilURL:url options:nilerror:nil];

    

    

    //创建上下文

    NSManagedObjectContext *context=[[NSManagedObjectContextalloc] init];

    context.persistentStoreCoordinator=psc;

   NSError * error;

    

    //************多表关联*************************************


    //1、插入教师数据

//    Teacher * teacher=[NSEntityDescription insertNewObjectForEntityForName:@"Teacher" inManagedObjectContext:context];

//    teacher.name=@"laoshi";

 

     //2、添加五个学生,并与表中已存在的上述老师建立联系

//    NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"Teacher"];

//    NSPredicate * pre=[NSPredicate predicateWithFormat:@"name='laoshi'"];

//    request.predicate=pre;

//    NSArray * array=[context executeFetchRequest:request error:nil];

//

//    NSMutableSet * set=[NSMutableSet set];

//    for (int i=0;i<5; i++) {

//        Student * student=[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context];

//        student.name=[NSString stringWithFormat:@"maoning%i",i];

//        student.age=@(13+i);

//        [set addObject:student];

//

//    }

///    Teacher * teacher=[array firstObject];

//    teacher.studentship=set;//一方建立了关系,另一方就不用再建立关系

   


    //3、查询学生

    

//    NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"Student"];

//    NSArray * array=[context executeFetchRequest:request error:nil];

//    

//    for (Student * stu in array) {

//        NSLog(@"%@,%@",stu.name,stu.teachership.name);

//        NSLog(@"***************************");

//    }

    

 

    //4、查询老师以及每一位老师对应的学生

//    NSFetchRequest * request=[[NSFetchRequest alloc] initWithEntityName:@"Teacher"];

//    NSArray * array=[context executeFetchRequest:request error:nil];

//

//    for (Teacher * tea in array) {

//        NSLog(@"------------------------");

//        NSLog(@"%@",tea.name);

//        NSSet * set=tea.studentship;

//

//        for (Student * stu in set) {

//            NSLog(@"%@",stu.name);

//        }

//        

//      NSLog(@"***************************");

//    }

//    *********************************

   


//    如果给已经存在的老师添加新的学生,原来的学生的关系会全部被顶替

//    正确的做法是:


    //    1.>取出老师以及对应的学生

    NSFetchRequest * request=[[NSFetchRequestalloc] initWithEntityName:@"Teacher"];

    NSPredicate * pre=[NSPredicatepredicateWithFormat:@"name='laoshi'"];

    request.predicate=pre;

   NSArray * array=[context executeFetchRequest:request error:nil];

   Teacher * teacher=[array firstObject];

   NSMutableSet * set=[NSMutableSetsetWithSet:teacher.studentship];

    //    2.>添加新学生

   for (int i=0;i<5; i++) {

        Student * student=[NSEntityDescriptioninsertNewObjectForEntityForName:@"Student"inManagedObjectContext:context];

        student.name=[NSStringstringWithFormat:@"maoning%i",i];

        student.age=@(13+i);

        [setaddObject:student];

    }

    teacher.studentship=set;//一方建立了关系,另一方就不用再建立关系

 

    //4、保存(相当于提交事务)

    [contextsave:&error];

    


}

0 0