gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:57 24 4
gpt4 key购买 nike

向核心数据添加数据时,如何检查关系是否已经建立?目前我的两个实体之间有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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com