为什么我需要在核心数据项目中使用原始访问器方法? [英] why would I need to use a primitive accessor methods in a core data project?

查看:86
本文介绍了为什么我需要在核心数据项目中使用原始访问器方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我需要在核心数据项目中使用原始访问器方法?



我正在阅读Core Data,并注意以下内容:

默认情况下,核心数据动态
创建高效的公共和原始的
get和设置访问器方法为
建模的属性(

)属性和
关系)
classes ...



例如,给定一个具有
属性firstName的实体数据
自动生成firstName,
setFirstName :, primitiveFirstName和
setPrimitiveFirstName:


仍然不确定原语访问器方法是什么?他们如何工作?当&为什么我需要在正常的访问器方法上使用它们?



感谢

解决方案

在普通类中,通常不会使用原始访问器,但Core Data经常使用它们。最常见的情况:您编写一个自定义访问器(执行维护,创建默认对象,处理持久性属性的瞬时包装等),但希望使用Core Data的优化存储。如果优化的访问器是正常的访问器,你的自定义访问器将阻止它的可用性,而是你只需要使用原始访问器。



从Xcode中获取包含方法来从多对多关系中添加或删除对象。如果这些方法使用正常访问器,则正常访问器将触发整个集合的改变通知。相反,他们使用原始访问器,并为他们正在改变的部分创建自己的更改通知。



最重要的是,原始访问器是唯一的方法到优化存储。没有它们,你必须使用Apple建议的实例变量。


Why would I need to use a primitive accessor methods in a core data project?

I'm reading about Core Data and note the below:

By default, Core Data dynamically creates efficient public and primitive get and set accessor methods for modeled properties (attributes and relationships) of managed object classes...

For example, given an entity with an attribute firstName, Core Data automatically generates firstName, setFirstName:, primitiveFirstName, and setPrimitiveFirstName:.

I'm still not sure what the primitive accessor methods are? How do they work? When & why would I need to use them over the normal accessor methods?

thanks

解决方案

In normal classes, you would not usually use primitive accessors, but Core Data uses them frequently. The most common scenario: You write a custom accessor (to perform maintenance, create a default object, handle transient wrapper of persistent property, etc.) but want to use Core Data's optimized storage. If the optimized accessors were normal accessors, your custom one would prevent it from being available, but instead you just have to use the primitive accessor.

Also, the default accessors you get from Xcode include methods to add or remove objects from to-many relationships. If these methods used normal accessors, the normal accessors would trigger change notifications for the entire set. Instead they use the primitive accessors and create their own change notifications for just the part they are changing.

The most important thing is that the primitive accessors are the only way to get to the optimized storage. Without them, you would have to use instance variables, which Apple advises against.

这篇关于为什么我需要在核心数据项目中使用原始访问器方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆