向核心数据添加数据时,如何检查关系是否已经建立?目前我的两个实体之间有TO MANY关系。
我正在尝试创建详细 View ,但遇到困难,我不确定这是由于未建立关系还是我的问题是将数据传递给新的 View Controller 。
我正在使用以下代码将数据添加到核心数据实体。在建立两者之间的关系时,这看起来合适吗?
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"];
它设置从routineEntityDetail
到routineEntity
的关系指针。
由于 routinedet
是routineinfo
的反向关系,routineEntityDetail
自动添加到 routineEntity
的 routinedet
关系中。
这没有意义:
[[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/