如何检查一个关系是否已经build立 – 核心数据

在将数据添加到核心数据时,如何检查是否build立了关系? 目前,我的两个实体之间有很多关系。

我正在尝试创build一个细节视图,但我正在挣扎,我不知道,如果它是由于没有build立关系,或者如果我的问题是将数据传递到新的视图控制器。

在这里输入图像说明

我正在使用下面的代码将数据添加到核心数据实体。 在build立两者之间的关系时,这看起来是否正确?

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; 

错误调查

我使用了一个build议的方法,但是当我testingNSLog(@"ExTitle *** %@",Ex.routinedet);的关系时NSLog(@"ExTitle *** %@",Ex.routinedet);我仍然得到这个错误NSLog(@"ExTitle *** %@",Ex.routinedet); routinedet是@property @property (nonatomic, retain) NSSet *routinedet; nonatomic @property (nonatomic, retain) NSSet *routinedet; 在核心数据生成的NSObject关系模型中:

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

我也testing过,以确保赛格正在工作,它是;

 self.title = Ex.routinename; RoutinesDetails *info; NSLog(@"Image *** %@",info.image); 

它将标题显示为正确的名称,但将图像string返回为空。

假设实体在核心数据详细信息视图中与关系一起定义,则以下代码将build立两个对象之间的关系:

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

它将关系指针从routineEntityDetailroutineEntity

由于routinedet相反的关系routineinfo routineEntityDetail会自动添加到routinedet关系中。

这根本不符合逻辑:

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

这看起来不错:

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

没有看到你的datamodel我不能确定,但​​我相信你会想这样的事情:

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

假定一个例程有许多例程细节,并且这个关系的命名方式是通过在XCode中生成NSManagedObject子类。 我还删除了类名中的复数名称,因为模型类通常是单数的。

如果我的假设是closures的,我表示歉意。

我编码的数据模型在这里: 在这里输入图像说明

Interesting Posts