does anyone have a working example of a fetched-property in core-data?

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

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.

  • NSManagedObject subclasses duplicate declaration
  • Core data: What should I type in the “Value Transformer Name”
  • Swift NSPredicate throwing EXC_BAD_ACCESS(Code=1, address=0x1) when compounding statements
  • How to list variables for NSManagedObject
  • Core Data in Swift: Only saving last object in a for loop
  • 'filenames are used to distinguish private declarations of the same name' error
  • 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).

    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.

    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.

    At this point I am giving up on this time-waster and simply implementing the fetch-request code myself.

    3 Solutions Collect From Internet About “does anyone have a working example of a fetched-property in core-data?”

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

    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.

    So, in the ‘Card’ object I put the Fetched Property ‘statsOfTypeOne’, with Destination set to ‘Stats’.

    In the predicate for this fetched property I put

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

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

    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"]

    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.


    Adding to @Peter’s answer. Here’s how I got it working in Swift 2.0 and Xcode 7:

    import Foundation
    import CoreData
    class Card: NSManagedObject {
        @NSManaged var statsOfTypeOne: [Stat]

    And then, to read the fetched property:

    managedObjectContext.refreshObject(someCard, mergeChanges: true)
    // This works and returns [Stat] type
    // So does this
    someCard.valueForkey("statsOfTypeOne") as! [Stat]

    Have you taken a look at this previous question: Xcode 4 Core Data: How to use fetched property created in Data Model editor

    Read through the accepted answer and all of the comments. It sounds like they have it sorted out.