How to check if a relationship has been established - Core Data

ⅰ亾dé卋堺 提交于 2019-12-06 03:15:35
Martin R

Assuming that the entities are defined as in Core Data Detail View with relationship, the following code establishes a relationship between the two objects:

[routineEntityDetail setValue:routineEntity forKey:@"routineinfo"];

It sets the relationship pointer from routineEntityDetail to routineEntity.

Since routinedet is the inverse relationship to routineinfo, routineEntityDetail is automatically added to the routinedet relationship of routineEntity.

This does not make sense:

[[routineEntityDetail valueForKey:@"name"] addObject:routineEntity];

And this looks OK:

[routineEntity setValue: info.name  forKey:@"routinename"];
[routineEntityDetail setValue: info.details.muscle  forKey:@"image"];

Without seeing your datamodel I can't be sure, but I believe you would want something like this:

ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
Routine  *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine"inManagedObjectContext:context];
RoutineDetail  *routineDetail = [NSEntityDescription insertNewObjectForEntityForName:@"RoutineDetail" inManagedObjectContext:context];

routine.routineName = info.name;
routineDetail.image = info.details.muscle;

[routine addRoutineDetailsObject:routineDetail];

That assumes that a routine has many routineDetails, and that the relationship is named as it would be by generating the NSManagedObject subclass in XCode. I also removed the plural name in the class names, as model classes are generally singular.

If my assumptions are off I apologize.

The datamodel I was coding for is here:

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!