如何检查是否已建立关系 - 核心数据 [英] How to check if a relationship has been established - Core Data

查看:132
本文介绍了如何检查是否已建立关系 - 核心数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何检查在向核心数据添加数据时是否建立了关系?目前我在两个实体之间有 TO MANY 关系。

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

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

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

确保segue正在工作,它是作为;

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

推荐答案

假设实体定义为核心数据详细信息视图与关系,以下代码建立
之间的关系两个对象:

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

它将关系指针从 routineEntityDetail 设置为 routineEntity

It sets the relationship pointer from routineEntityDetail to routineEntity.

由于 routinedet 反向关系 c $ c> routineinfo routineEntityDetail
会自动添加到 routinedet

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

这不合理:

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

这看起来不错:

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

这篇关于如何检查是否已建立关系 - 核心数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆