CoreData RelationShips设置RelationShip类型

时间:2015-12-10 06:53:08

标签: ios core-data

我刚试过CoreData Relation Ships 它工作正常,但

两个实体已连接因此,如果要从一个实体删除数据,它应该反映连接实体应该怎么做?

下面的代码Givn

    AppDelegate *delegate = [[UIApplication sharedApplication]delegate];

NSManagedObjectContext *context = [delegate managedObjectContext];
Person *per = [NSEntityDescription
               insertNewObjectForEntityForName:@"Person"
                                  inManagedObjectContext:context];
per.name = @"Test Name";
per.age=@"age";
Medicine *med = [NSEntityDescription
                 insertNewObjectForEntityForName:@"Medicine"
                                        inManagedObjectContext:context];
med.name= @"paracetmol";
per.meddet = med;
med.patdet = per;
NSError *error;
if (![context save:&error]) {
    NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}else{
    NSLog(@"ok");
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person"
                                          inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Person *info in fetchedObjects) {
    NSLog(@"Name: %@", info.name);
     NSLog(@"%@",info.meddet.name);
}

1 个答案:

答案 0 :(得分:0)

使用deleteRule Cascade

设置从person到med的关系