ios - 如何检查关系是否已经建立 - Core Data

标签 ios core-data nsmanagedobject nsentitydescription

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

我正在尝试创建详细 View ,但遇到困难,我不确定这是由于未建立关系还是我的问题是将数据传递给新的 View Controller 。

enter image description here

我正在使用以下代码将数据添加到核心数据实体。在建立两者之间的关系时,这看起来合适吗?

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;

错误调查:

我使用了其中一种建议的方法,但当我在 NSLog(@"ExTitle *** %@",Ex.routinedet); 中测试关系时,我仍然遇到此错误,其中 routinedet 是@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";

我还进行了测试以确保 segue 正常工作并且它是这样;

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

将标题显示为正确的名称,但将图像字符串返回为空。

最佳答案

假设实体定义为 Core Data Detail View with relationship ,下面的代码建立了两者之间的关系 两个对象:

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

它设置从routineEntityDetailroutineEntity的关系指针。

由于 routinedetroutineinfo反向关系routineEntityDetail 自动添加到 routineEntityroutinedet 关系中。

这没有意义:

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

这看起来不错:

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

关于ios - 如何检查关系是否已经建立 - Core Data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17557288/

相关文章:

iphone - 如何将您的应用添加到通知中心?

ios - 无法在 swift2 中声明类扩展

ios - 如何重新创建 persistentStore 不受更改 .xcdatamodeld 的影响

objective-c - MagicalRecord importFromObject : JSON with dictionary?

ios - 使用谓词获取总是返回 nil

ios - 如何保持固定元素的子位置固定? (iOS 网页)

ios - 如何在 iOS 中使用 for 循环以编程方式添加 UIButton?

ios - 在 View Controller 之间传递 NS Core Data 上下文引用

objective-c - NSManagedObjectContextObjectsDidChangeNotification NSDeletedObjectsKey 对象不再引用相关对象

ios - 原因: 'An NSManagedObject of class ' NSManagedObject' must have a valid NSEntityDescription?