有没有人有一个核心数据中的获取属性的工作示例? [英] does anyone have a working example of a fetched-property in core-data?

查看:100
本文介绍了有没有人有一个核心数据中的获取属性的工作示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试使用抓取的属性几次,虽然它似乎是正确的方法,它永远不会工作

I have tried to use fetched properties a couple of times, and although it seems to be the right approach, it never works.

在我最近的尝试中,我添加了fetched属性到我的实体,选择模型中的其他实体作为目的地,并将谓词设置为我知道有效的条件。

In my latest attempt I added the fetched-property to my entity, selected the other entity in the model as the 'destination', and set the predicate to a condition that I know is valid.

问题1:当生成实体的NSManagedObject类时,它不包括fetched-property的任何内容。一些搜索后,我在.h文件中添加了声明,在.m文件中添加了@dynamic语句(是的,我知道它是一个NSArray *类型)。

Problem 1: When the NSManagedObject-class for the entity is generated it does not include anything for the fetched-property. After some searching I added the declaration for it in the .h file and the @dynamic statement for it in the .m file (yes, I know it's an NSArray * type).

问题2:即使之后,当我访问这个属性在代码中,我得到一个异常抛出的状态,大意是fetch-request没有一个实体。我假设实体是指定为目的地的实体,并且实际上是在那里。

Problem 2: Even after that, when I access this property in code I get an exception being thrown that states something to the effect that the fetch-request does not have an entity. I am assuming that the 'entity' would be the one specified as the 'destination' and it is, in fact, there.

所以,我想要一个人提供一个具体的工作示例(iOS平台),其中在模型中定义了一个获取的属性,在NSManagedObject派生类中声明,并实际从代码中使用。

So, I'd like someone to provide a concrete working example (iOS platform) where a fetched-property is defined in the model, declared in a NSManagedObject-derived class, and actually used from code.

推荐答案

这里是我的相关位(包括你已经提到的位):

Here's my relevant bits of code (including bits you've already mentioned):

我的例子有一个'Card'对象,与'Stats'对象有1->多关系。每个Stats对象都有一个结果,可以是1-4。我获取的属性是一个简单的,给我的'卡'对象一个'统计'对象的数组,只有'结果'= 1。

My example has a 'Card' object that has a 1->many relationship with a 'Stats' object. Each 'Stats' object has an 'outcome' that can be 1-4. My fetched property is a simple one to give my 'Card' object an array of 'Stats' objects that are of 'outcome'=1 only.

获取的属性,以便我可以很容易地抓住具有超过一定数量和种类的'统计'对象的'卡'对象。

I wanted to use the fetched property so that I could easily get hold of 'Card' objects that had more than a certain number and kind of 'Stats' objects.

在这个获取的属性的谓词中,我放置了

In the predicate for this fetched property I put

(SELF.outcome=1) AND (SELF.card=$FETCH_SOURCE)

'SELF'是'stats'记录,$ FETCH_SOURCE在执行时成为'Card'对象。

'SELF' is the 'stats' record, and $FETCH_SOURCE magically becomes the 'Card' object when executed.

正如你所做的,我把'Card'对象的.h和.m文件中的以下内容:

As you did, I put the following in the .h and .m files for the 'Card' object:

@property (nonatomic, retain) NSArray *statsOfTypeOne;
@dynamic statsOfTypeOne;

然后在我的代码中我使用:

Then in my code I used:

[self.managedObjectContext refreshObject:cardInstance mergeChanges:YES];
[cardInstance valueForKey:@"statsOfTypeOne"]

.statsOfTypeOne应该很好)。没有刷新对象,它没有更新Fetched属性(根据手册)。

to get at the array (although cardInstance.statsOfTypeOne should be fine). Without the refresh object it wasn't updating the Fetched property (as per the manual).

我这是我做的一切这行得通。请告诉我是否适合您。

I think that's everything that I did to make it work. Let me know if it works for you.

彼得

这篇关于有没有人有一个核心数据中的获取属性的工作示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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