官方文档建议为每一个可以设置inverse的relationship设置一个inverse。目的是保持数据库的正确性。但是没有具体的说明。

我在stackoverflow中找到了一个是分好的答案,http://stackoverflow.com/questions/764125/does-every-core-data-relationship-have-to-have-an-inverse

内容如下:

Apple documentation has an great example that suggest a situation where you might have problems by not having inverse relationship. Lets map it into this case.

Lets Assume you modeled it as follows

Note you have to-one relationship called "type", from SocialApp to SocialAppType. The relationship is non-optional and has a "deny" delete rule.

Now Lets consider following Code.

SocialApp *socialApp;
SocialAppType *appType;
// assume entity instances correctly instantiated [socialApp setSocialAppType:appType];
[managedObjectContext deleteObject:appType];
BOOL saved = [managedObjectContext save:&error];

What we expect is to fail this context save since we have set the delete rule as Deny while relationship is non optional.

But here the save succeeds.

Reason is we haven't set a inverse relationship. Because of that socialApp instance not get marked as changed when appType is deleted. So no validation happens for socialApp before saving (It assumes no validation needed since no change happened). But actually a change happened. But it doesn't get reflected.

if we recall appType by

SocialAppType *appType = [socialApp socialAppType];

appType is nil.

So weird isn't it. get nil for non optional attribute?

So you are in no trouble if you have set up the inverse relationship. Otherwise you have to do force validation by writing the code as follows.

SocialApp *socialApp;
SocialAppType *appType;
// assume entity instances correctly instantiated [socialApp setSocialAppType:appType];
[managedObjectContext deleteObject:appType]; [socialApp setValue:nil forKey:@"socialAppType"]
BOOL saved = [managedObjectContext save:&error]; 最近在使用coredata 时 发现了一个inverse的作用:
比如有 profile 和 score 表,它们之间是1对1关系,我们先建立了一个profile,之后想设置他的score,我们只需要调用 score.profile = profile 或者 profile.score = score 即可,
不需要2者同时调用,就可以建立起他们的关联。比如我们掉用了
score.profile = profile 那么 对应profile 的score属性会自动设置!

iOS CoreData relationship 中的inverse的更多相关文章

  1. IOS CoreData 多表查询demo解析

    在IOS CoreData中,多表查询上相对来说,没有SQL直观,但CoreData的功能还是可以完成相关操作的. 下面使用CoreData进行关系数据库的表与表之间的关系演示.生成CoreData和 ...

  2. IOS CoreData 多表查询(下)

    http://blog.csdn.net/fengsh998/article/details/8123392 在iOS CoreData中,多表查询上相对来说,没有SQL直观,但COREDATA的功能 ...

  3. iOS CoreData技术学习资源汇总

    一.CoreData学习指引 1. 苹果官方:Core Data Programming Guide 什么是CoreData? 创建托管对象模型 初始化Core Data堆栈 提取对象 创建和修改自定 ...

  4. iOS CoreData (一) 增删改查

    代码地址如下:http://www.demodashi.com/demo/11041.html Core Data是iOS5之后才出现的一个框架,本质上是对SQLite的一个封装,它提供了对象-关系映 ...

  5. iOS CoreData (二) 版本升级和数据库迁移

    前言:最近ChinaDaily项目需要迭代一个新版本,在这个版本中CoreData数据库模型上有新增表.实体字段的增加,那么在用户覆盖安装程序时就必须要进行CoreData数据库的版本升级和旧数据迁移 ...

  6. iOS CoreData 介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  7. iOS CoreData介绍和使用(以及一些注意事项)

    iOS CoreData介绍和使用(以及一些注意事项) 最近花了一点时间整理了一下CoreData,对于经常使用SQLite的我来说,用这个真的有点用不惯,个人觉得实在是没发现什么亮点,不喜勿喷啊.不 ...

  8. iOS 解决LaunchScreen中图片加载黑屏问题

    iOS 解决LaunchScreen中图片加载黑屏问题 原文: http://blog.csdn.net/chengkaizone/article/details/50478045 iOS 解决Lau ...

  9. iOS开发 Xcode8中遇到的问题及改动

      iOS开发 Xcode8中遇到的问题及改动 新版本发布总会有很多坑,也会有很多改动. 一个一个填吧... 一.遇到的问题 1.权限以及相关设置 iOS10系统下调用系统相册.相机功能,或者苹果健康 ...

随机推荐

  1. Mongodb3.0.6副本集+分片学习笔记

    一.使用问题记录 1. mongodb3.0.6使用mongostat参数 >./mongostat -h 127.0.0.1:27017 -u root -p 123456 /authenti ...

  2. centos yum 安装

    LINUX下YUM源配置 1.确保RHEL5中已经安装了yum [root@lvs-master ~]# rpm -qa |grep yumyum-metadata-parser-1.1.2-3.el ...

  3. 从零学习storm(一) 环境的安装配置

    1.首先 安装zookeeper   2.安装Java环境   3.安装Python   下载python包,编译安装     1.解压      2.configure     3.make     ...

  4. NERD_commenter——VIM批量注释与反注释插件

    转自:http://www.xefan.com/archives/83568.html 这是对程序员非常实用的一款插件,支持多种语言的补全,还支持单行注释,批量注释,等各种命令映射. 使用方法,先下载 ...

  5. linux shell中判断bash脚本输入的参数个数

    看下面的一段程序. #!/bin/bash ]; then echo "参数个数为$#个" else echo "没有参数" fi

  6. C/C++使用HTTP协议上传

    上传文件: http://zengrong.net/post/2088.htm #include <stdio.h> #include <string.h> #include ...

  7. 【Solr】 solr对拼音搜索和拼音首字母搜索的支持

    问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的.  用lucene实现 1.建索引, 多建一个索引字段 ...

  8. Excel 使用CHIINV函数和GAMMA.DIST函数绘制卡方分布

    1.使用CHIINV(概率,自由度),在Excel中绘制卡方分布. 若n个独立的随机变量均服从标准正态分布,则这n个随机变量的平方和构成一新的随机变量,其分布规律称为服从自由度为ν 的χ2分布. 2. ...

  9. CodeForces 353B Two Heaps

    B. Two Heaps   Valera has 2·n cubes, each cube contains an integer from 10 to 99. He arbitrarily cho ...

  10. 关系型数据库ACID

    关系型数据库ACID 一.事务 定义:所谓事务,它是一个操作序列,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位. 准备工作:为了说明事务的ACID原理,我们使用银行账户及资金管理的案例 ...