0

我正在使用核心数据将数据存储在我的 iOS 应用程序中。有两个实体,一个实体与另一个实体具有一对多关联(例如 1 门课程 -> 许多学生,1 个学生 -> 许多年级),反之亦然。

每当我创建学生(或年级)的新实例并通过课程(或学生)(NSManagedObject)时,此引用无法保存并变为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 course。你们有什么想法,为什么这不起作用?

提前致谢!

4

1 回答 1

0

或许你可以同时尝试建立反比关系,即在_detailItem中加入newstudent。我昨天用这种方式解决了一个类似的问题,但真的不知道为什么。

于 2013-11-21T16:21:09.333 回答