views:

328

answers:

2

Hi

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 Gallery model which would return one and only one Image object.

Is there a way to do this with fetched properties?

+3  A: 

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.

Joshua Nozzi
A: 

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.

Alex