定制核心数据生成的访问器 [英] Customizing core data generated accessors

查看:112
本文介绍了定制核心数据生成的访问器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置了一个简单的核心数据模式,其中项对象被添加到列表对象。在生成类之后,核心数据生成了标准访问器,包括向列表中添加项目的几种方法(list.addItemsObjects和list.addItems:)。



I还要向每个项目添加dateAddedNSDate。现在,我可以手动设置每次我创建一个对象,因为核心数据将为我提供一个item.dateAdded访问器。但真的,我宁愿这是由列表本身处理,因为实现将永远不会改变。所以每次我调用list.AddItemObject,我想它同时设置项的dateAdded属性。我想我也需要做相同的逆,所以item.setList也设置dateAdded。



我不知道如何做到这一点。我应该尝试重新定义现有的核心数据生成的访问器?如果是这样,我该怎么做(是否有某处调用我的自定义代码中的原始实现,使得反向关系被处理,以及任何其他核心数据必需品)?

解决方案

您可以覆盖核心数据生成的访问器。
你必须注意几个特殊的事情,比如调用 willChangeValueForKey didChangeValueForKey ,但除​​此之外



阅读Apple文档: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html p>

这里有很多例子。只要注意一对一和一对多关系之间的区别。



您不需要实现反向功能,这是由Core完成的数据。如果调用 list.AddItem ,则会自动为您调用 item.setList



所以只需要添加你想要的代码到 item.setList 存取器。这样,您可以确保每次将某个项目添加到列表中时,相应地更新日期。



您的代码将类似于:

   - (void)setList:(List *)value 
{
[self willChangeValueForKey:@list];
[self setPrimitiveValue:[NSDate date] forKey:@dateAdded]; // use setValue:forKey:if you need KVO for dateAdded
[self setPrimitiveValue:value forKey:@list];
[self didChangeValueForKey:@list];
}

EDIT 您可能想知道以下内容:



如果您自定义列表方法(即 addItemObject ),还需要自定义 setItems 。此外,如果您的项目已添加到不是您自定义的列表的列表中,您的自定义代码当然不会调用。



另一方面,如果您自定义 setList 方法,如果项目没有更改,则代码不会调用



例如,如果调用

  [list addItems:[NSSet setWithObject:item]]; 
[list addItemsObject:item];

那么该项目的 setList 调用一次!直接调用 item.setList 始终调用,即使没有任何更改。


I have set up a simple core data schema, where item objects are added to list objects. After generating classes, core data has generated the standard accessors, including a couple of ways to add items to a list (list.addItemsObjects and list.addItems:).

I also want to add a 'dateAdded' NSDate to each item. Now, I could manually set this every time I create an object, since core data will provide me with an item.dateAdded accessor. But really, I would prefer that this is handled by the list itself, since the implementation will never change. So every time I call list.AddItemObject, I would like it to set the dateAdded attribute of the item at the same time. I guess I would also need to do the same in the inverse, so that item.setList also sets the dateAdded.

I am not sure how to do this. Should I try to redefine the existing core data-generated accessors? If so, how do I do this (is there someway to call the original implementation inside my custom code, such that the inverse relationship is handled, and any other core data necessities)? Or is there some better way to customize these methods?

解决方案

You can override the core data generated accessors. You have to pay attention to a few special things like calling willChangeValueForKey and didChangeValueForKey, but other than that overriding the accessors is pretty much the same as always.

Read the Apple documentation: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdAccessorMethods.html

It's explained clearly with lots of examples. Just pay attention to the difference between one-to-one and one-to-many relations.

You do not need to implement the inverse functionality, this is done by Core Data. If you call list.AddItem, then item.setList will automatically be called for you.

So just add your desired code to the item.setList accessor. That way, you guarantee that everytime an item is added to a list the date is updated accordingly. Don't bother with the list-accessors.

Your code would be something like:

- (void)setList:(List *)value
{
    [self willChangeValueForKey:@"list"];
    [self setPrimitiveValue:[NSDate date] forKey:@"dateAdded"]; // use setValue:forKey: if you need KVO for dateAdded
    [self setPrimitiveValue:value forKey:@"list"];
    [self didChangeValueForKey:@"list"];
}

EDIT I've given it another thought and you might want to know the following:

If you customize the list methods (ie addItemObject), you'll also need to customize setItems. Also, if your item is added to a list which happens not to be the list you customized, your custom code is of course not called.

On the other hand, if you customize the setList method, the code is not called if there are no changes for the item.

For example, if you call

[list addItems:[NSSet setWithObject:item]];
[list addItemsObject:item];

then the item's setList accessor will only be called once! Calls directly to item.setList are always called, even if nothing changed.

这篇关于定制核心数据生成的访问器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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