How would you check if a relationship has been established when adding data to core data? Currently I have a TO MANY relationship between two of my entities.
I am attempting to create a detail view but am struggling and i'm not sure if its due to a relationship not being established or if my issue is with passing the data over to the new view controller.
I am adding the the data to the core data entities using the following code. Does this look right when establishing a relationship between the two?
ExcerciseInfo *info = [_fetchedResultsController objectAtIndexPath:indexPath];
NSManagedObject *routineEntity = [NSEntityDescription insertNewObjectForEntityForName:@"Routines"inManagedObjectContext:context];
NSManagedObject *routineEntityDetail = [NSEntityDescription insertNewObjectForEntityForName:@"RoutinesDetails" inManagedObjectContext:context];
[[routineEntityDetail valueForKey:@"name"] addObject:routineEntity];
[routineEntity setValue: info.name forKey:@"routinename"];
[routineEntityDetail setValue: info.details.muscle forKey:@"image"];
NSError *error = nil;
Error Investigation:
I used one of the suggested methods but am still getting this fault when i tested the relationship in a NSLog(@"ExTitle *** %@",Ex.routinedet);
with routinedet being the @property (nonatomic, retain) NSSet *routinedet;
in the core data generated NSObject relationship model:
Relationship 'routinedet' fault on managed object (0x749ea50) <Routines: 0x749ea50> (entity: Routines; id: 0x749c630 <x-coredata://C075DDEC-169D-46EC-A4B7-972A04FCED70/Routines/p1> ; data: {
routinedet = "<relationship fault: 0x8184a20 'routinedet'>";
routinename = "Leg Crunch";
I have also tested to make sure the segue is working and its is as;
self.title = Ex.routinename;
RoutinesDetails *info;
NSLog(@"Image *** %@",info.image);
which shows the title as the correct name but returns image string as null.
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"];