有没有办法更改核心数据属性的默认值

时间:2020-11-29 17:03:11

I would like to change the default value of some of the attributes in my core data model dynamically.

我想动态更改核心数据模型中某些属性的默认值。

For instance, my app deals with real estate investment, and I have an attribute pertaining to interest rate (the type Float). If the user enters an interest rate of 3.5% (float value would be 3.5) for a particular property they are analyzing, I would like the value for the next property they analyze to automatically populate with 3.5.

例如,我的应用程序处理房地产投资,我有一个与利率相关的属性(Float类型)。如果用户为他们正在分析的特定属性输入3.5%的利率(浮动值为3.5),我希望他们分析的下一个属性的值自动填充3.5。

Is there a way to accomplish this without subclassing NSManagedObject?

有没有办法在没有子类化NSManagedObject的情况下实现这一目标?

1 个解决方案

#1


6  

Good question - by default, managed objects are initialized with the default values given in the managed object model. But like you say, sometimes you might want a dynamic default value (the example Apple use in their own documentation is using the current date/time as a default value).

好问题 - 默认情况下,托管对象使用托管对象模型中给出的默认值进行初始化。但是就像你说的,有时你可能想要一个动态默认值(Apple在他们自己的文档中使用的例子是使用当前日期/时间作为默认值)。

Unfortunately I don't believe there's a way to do this without subclassing NSManagedObject. There's an Apple recommended way to do this - rather than overriding the init method (not recommended), you instead use the awakeFromInsert method, which is called when the object in question is first inserted into the managed object context.

不幸的是,我不相信没有子类化NSManagedObject就可以做到这一点。有一种Apple建议的方法 - 而不是覆盖init方法(不推荐),而是使用awakeFromInsert方法,当有问题的对象首次插入到托管对象上下文时调用该方法。

Here's what Apple say from their own docs:

以下是Apple在他们自己的文档中所说的话:

awakeFromInsert:

You typically use this method to initialize special default property values. This method is invoked only once in the object's lifetime.

您通常使用此方法初始化特殊的默认属性值。此方法仅在对象的生命周期中调用一次。

If you want to set attribute values in an implementation of this method, you should typically use primitive accessor methods (either setPrimitiveValue:forKey: or—better—the appropriate custom primitive accessors). This ensures that the new values are treated as baseline values rather than being recorded as undoable changes for the properties in question.

如果要在此方法的实现中设置属性值,通常应使用原始访问器方法(setPrimitiveValue:forKey:或更好 - 适当的自定义原始访问器)。这可确保将新值视为基准值,而不是将其记录为相关属性的可撤消更改。

So to answer your original question - I can't think of a way to do this without subclassing NSManagedObject, and subclassing is the officially recommended approach for handling dynamic default values.

所以回答你原来的问题 - 如果没有子类化NSManagedObject,我想不出这样做的方法,而子类化是官方推荐的处理动态默认值的方法。

#1


6  

Good question - by default, managed objects are initialized with the default values given in the managed object model. But like you say, sometimes you might want a dynamic default value (the example Apple use in their own documentation is using the current date/time as a default value).

好问题 - 默认情况下,托管对象使用托管对象模型中给出的默认值进行初始化。但是就像你说的,有时你可能想要一个动态默认值(Apple在他们自己的文档中使用的例子是使用当前日期/时间作为默认值)。

Unfortunately I don't believe there's a way to do this without subclassing NSManagedObject. There's an Apple recommended way to do this - rather than overriding the init method (not recommended), you instead use the awakeFromInsert method, which is called when the object in question is first inserted into the managed object context.

不幸的是,我不相信没有子类化NSManagedObject就可以做到这一点。有一种Apple建议的方法 - 而不是覆盖init方法(不推荐),而是使用awakeFromInsert方法,当有问题的对象首次插入到托管对象上下文时调用该方法。

Here's what Apple say from their own docs:

以下是Apple在他们自己的文档中所说的话:

awakeFromInsert:

You typically use this method to initialize special default property values. This method is invoked only once in the object's lifetime.

您通常使用此方法初始化特殊的默认属性值。此方法仅在对象的生命周期中调用一次。

If you want to set attribute values in an implementation of this method, you should typically use primitive accessor methods (either setPrimitiveValue:forKey: or—better—the appropriate custom primitive accessors). This ensures that the new values are treated as baseline values rather than being recorded as undoable changes for the properties in question.

如果要在此方法的实现中设置属性值,通常应使用原始访问器方法(setPrimitiveValue:forKey:或更好 - 适当的自定义原始访问器)。这可确保将新值视为基准值,而不是将其记录为相关属性的可撤消更改。

So to answer your original question - I can't think of a way to do this without subclassing NSManagedObject, and subclassing is the officially recommended approach for handling dynamic default values.

所以回答你原来的问题 - 如果没有子类化NSManagedObject,我想不出这样做的方法,而子类化是官方推荐的处理动态默认值的方法。