【问题标题】:How to check if a relationship has been established - Core Data如何检查关系是否已建立 - Core Data
【发布时间】:2013-07-09 20:10:53
【问题描述】:

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

我正在尝试创建一个详细视图,但我正在苦苦挣扎,我不确定这是由于未建立关系还是我的问题是将数据传递给新的视图控制器。

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

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 是核心数据生成的NSObject 关系模型中的@property (nonatomic, retain) NSSet *routinedet;

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

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

【问题讨论】:

    标签: ios core-data nsmanagedobject nsentitydescription


    【解决方案1】:

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

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

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

    因为routinedet 是与routineinfo反比关系,所以routineEntityDetail 自动添加到routineEntityroutinedet 关系中。

    这没有意义:

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

    这看起来不错:

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

    【讨论】:

    • 再次感谢马丁。所以目前它不会工作?不需要[[routineEntityDetail valueForKey:@"name"] addObject:routineEntity]; 吗?
    • @user2221799: nameRoutineDetails 的属性,而不是关系。
    • 所以在此基础上我会简单地将其更改为rountineinfo 以匹配关系。谢谢
    • @user2221799:是的,但是routineinfo 是一对一的关系,addObject 在这里没有意义。我的答案中的第一个代码显示了如何设置关系。
    • 辉煌。你今天对马丁很有帮助
    【解决方案2】:

    没有看到你的数据模型我不能确定,但​​我相信你会想要这样的东西:

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

    假设一个例程有很多routineDetails,并且通过在XCode中生成NSManagedObject子类来命名关系。我还删除了类名中的复数名称,因为模型类通常是单数的。

    如果我的假设不成立,我深表歉意。

    我编写的数据模型在这里:

    【讨论】:

    • 数据模型可以在本文开头看到 [link]stackoverflow.com/questions/17553654/… 。这是您的预期吗?
    • 你的不遵循我习惯的命名约定,但这不是错误交易。我觉得更大的问题是 NSManagedObject 子类的生成。在 XCode 中,当您查看数据模型时,您会在编辑菜单上找到该工具。
    • 但是您没有在代码中引用子类。我的意思是生成以您的实体命名的 .h 和 .m 接口和实现文件。但是,如果你让它工作并且对它感到满意,那么我们就完成了。 :-)
    猜你喜欢
    • 1970-01-01
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-03
    • 2020-03-31
    • 2019-12-25
    相关资源
    最近更新 更多