Xcode 4 Core Data: How to use fetched property created in Data Model editor

18,530

From what I've read you need to add fetched properties to the generated class yourself using the @dynamic keyword

// In your header
@property (nonatomic, retain) NSArray *fetchLastPage;

// In your class
@dynamic fetchLastPage;
Share:
18,530

Related videos on Youtube

Trần Trung Hiếu
Author by

Trần Trung Hiếu

I work in networking and telecommunications. I write mobile software. I have a puppy. Interested in: Python, Google App Engine, Objective-C, Cocoa, iPhone programming, Networking, Tcl, Perl, Tk, Java, Android, and pups.

Updated on July 10, 2022

Comments

  • Trần Trung Hiếu
    Trần Trung Hiếu almost 2 years

    How do you implement a fetched property in Xcode 4?

    Here is an example of two entities, a book and a page: enter image description here

    I followed the guidelines here to create a fetched property that references a value from the source entity using the variable $FETCH_SOURCE: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CoreData/Articles/cdRelationships.html

    Now, once I have this saved and I generate the source code I get this:

    //  Book.h
    
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    
    @class Pages;
    
    @interface Book : NSManagedObject {
    @private
    }
    @property (nonatomic, retain) NSString * title;
    @property (nonatomic, retain) NSNumber * pageCount;
    @property (nonatomic, retain) Pages * pages;
    
    @end
    

    And...

    //  Book.m
    
    #import "Book.h"
    #import "Pages.h"
    
    
    @implementation Book
    @dynamic title;
    @dynamic pageCount;
    @dynamic pages;
    
    @end
    

    Where is the fetched property 'fetchLastPage'? How can I use it in code?

    • Trần Trung Hiếu
      Trần Trung Hiếu almost 13 years
      FYI, this is not the actual code I am working with. I am writing an app where I have multiple data stores (one for read only and one for user saved data) and I need to use fetched properties since multiple stores do not maintain relationships between themselves: developer.apple.com/library/mac/documentation/Cocoa/Conceptu‌​al/…
  • TechZen
    TechZen almost 13 years
    +1 You've got the basic idea correct but a fetched property is always of the class NSArray. I will edit your answer to reflect that.
  • TechZen
    TechZen almost 13 years
    I will add that you have to do this by hand only because X4 Core Data tools are seriously buggy. In previous versions, it would automatically add the fetchedProperty definitions.
  • Moshe
    Moshe almost 13 years
    @TechZen - You should file a bug report with Apple.
  • TechZen
    TechZen almost 13 years
    @Moshe -- yeah, it's on my long, long list of bugs.
  • Trần Trung Hiếu
    Trần Trung Hiếu almost 13 years
    Thanks everyone, I will accept this answer once I try it out later today. Does anyone know of a good reference for fetched properties? I've looked at 3 different books on Core Data (probably the only 3 books on Core Data) and there is a serious lack of mentioning how to deal with fetched properties and utilizing multiple stores. I have not seen any examples of how to maintain loose relationships between entities distributed across multiple stores using fetched-properties.
  • Trần Trung Hiếu
    Trần Trung Hiếu almost 13 years
    @TechZen, I recently reinstalled Xcode3 and it did not create the fetched property.
  • TechZen
    TechZen almost 13 years
    @pokstad -- well, I know one of the earlier versions did. Fetched properties aren't that complicated, they just an array of objects fetched. The only real gotcha is that the fetch only runs when object first faults in. After that, you need to call refreshObject:mergeChanges: to get it to refresh.
  • Trần Trung Hiếu
    Trần Trung Hiếu almost 13 years
    Still testing to verify the answer. I've been tearing down relationships and replacing them with fetched properties and verifying the autogenerated source code is correct. Hopefully I'll finish tonight and accept the answer.
  • Trần Trung Hiếu
    Trần Trung Hiếu over 12 years
    Holy crap, it worked! Sorry for the long delay in answering. I had to tear down a lot of old code and fix some things before I could get to the point of testing this. Thank you everyone. BTW, I had an interesting issue with trying to separate data stores that I asked about here in case anyone wants to try something similar: stackoverflow.com/questions/7141333/…
  • Jonny
    Jonny over 9 years
    What object are you supposed to refresh in refreshObject? Is it the containing Entity or the fetched property itself? I have a problem where the fetched property array is always nil the first time the app is installed on device and everything is setup. On next launch the property array seems fine with a valid result. I always refreshObject: before I access the fetched property.