Ios CoreData无法保存引用对象
我正在使用核心数据将数据存储在iOS应用程序中。有两个实体,其中一个实体与另一个实体有一对多的关联(例如,一门课程->多个学生,一个学生->多个年级),反之亦然 每当我创建学生(或年级)的新实例并通过课程(或学生)(NSManagedObject)时,此引用将无法保存并变为Ios CoreData无法保存引用对象,ios,core-data,reference,Ios,Core Data,Reference,我正在使用核心数据将数据存储在iOS应用程序中。有两个实体,其中一个实体与另一个实体有一对多的关联(例如,一门课程->多个学生,一个学生->多个年级),反之亦然 每当我创建学生(或年级)的新实例并通过课程(或学生)(NSManagedObject)时,此引用将无法保存并变为null。没有例外,该对象的所有其他值都已保存,但对课程的引用为null 我在做什么: NSEntityDescription *entity = [NSEntityDescription entityForName:@"St
null
。没有例外,该对象的所有其他值都已保存,但对课程的引用为null
我在做什么:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:context];
Student *newStudent = (Student *)[[NSManagedObject alloc]initWithEntity:entity insertIntoManagedObjectContext:context];
[newStudent setLastname:_lastname.text];
[newStudent setFirstname:_firstname.text];
[newStudent setNote:_note.text];
NSNumber *tmp = [NSNumber numberWithInt:(int)[[(AppDelegate *)[[UIApplication sharedApplication]delegate] staticDataObject]increaseGlobalSchuelerID]];
[newStudent setId:tmp];
[newStudent setCourse:(Course *)_detailItem];
[newStudent setDesktopID:[NSNumber numberWithInt:-1]];
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
_detailItem是实体课程
的NSManagedObject。你们知道为什么这样不行吗
提前谢谢 也许您可以同时尝试建立反向关系,这意味着将newstudent添加到_detailItem中。昨天我用这种方法解决了一个类似的问题,但真的不知道为什么。你确定
\u detailItem
是一个有效的参考?在保存之前,您是否可以记录学生和课程(\u detailItem
)?