Вопрос

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.

enter image description here

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"];

Другие советы

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: enter image description here

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top