开发者

Setting a limit to a fetched property in Core Data

I have a one to many relationship between two objects, lets call them Gallery and Images. Each Image belongs to a Gallery and each Gallery has many Images.

I would like to add a fetched property to my Galler开发者_如何转开发y model which would return one and only one Image object.

Is there a way to do this with fetched properties?


For a fetched property, a predicate is your only option.

See the Predicate Programming Guide - Aggregate Operations section. You'll want to use array[FIRST].

Note, you'll likely get a different image each time, since there is no support for ordered sets in Core Data. You'd normally get around this by maintaining your Images' sort order in a "sortOrder" key and setting sort descriptors on your fetch, but I don't think it's possible to give sort descriptors on a fetched property.

Update for Lion: Support for ordered sets has been added to Core Data in 10.7 and above, making an extra "sortOrder" attribute unnecessary for apps targeting 10.7 and up.


A fetched property is represented by the NSFetchedPropertyDescription class. You can modify properties in code up until the point when the managed object model is actually used. So, in the code that loads up your managed object model, you can find your fetched property description and replace the fetch request with something that better matches what you're trying to do. You should be able to set a fetch limit on it in this way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜