ios - why did unmodeled property fire the fault? -


as know nsfetchrequest's propertiestofetch can limite properties want, can reduce footprint of memory. if this:

request.propertiestofetch = [nsarray arraywithobjects:@"nid", nil]; 

and:

    nsstring *strid = [nsstring stringwithformat:@"%@", aperson.nid]; cell.textlabel.text = strid; 

then debug output(and not fire fault):

coredata: sql: select t0.z_ent, t0.z_pk, t0.znid zperson t0 order t0.znid 

a strange thing happened, however, when add new unmodeled property subclass of nsmanagedobject person. this:

@interface person (ex) @property (nonatomic, retain) nsstring *strtempname; @end  @implementation person (ex) @dynamic strtempname;  -(void)setstrtempname:(nsstring *)strtempname {     [self setprimitivevalue:strtempname forkey:@"strtempname"]; }  -(nsstring*)strtempname {     return [self primitivevalueforkey:@"strtempname"]; } 

and new unmodeled property accessed:

nsstring *strtt = aperson.strtempname; 

then debug output:

2013-05-06 10:43:07.789 testcoredata[988:c07] coredata: sql: select 0, t0.z_pk, t0.z_opt, t0.zbisfortest, t0.zbisintrash, t0.znid, t0.zstrname, t0.zstrok, t0.zstrok2, t0.zstrtest1, t0.zstrtest2, t0.zadepart, t0.zamiddlethumnail, t0.zanormalpic, t0.zasmallthumnail zperson t0  t0.z_pk = ?  2013-05-06 10:43:07.790 testcoredata[988:c07] coredata: annotation: sql connection fetch time: 0.0010s 2013-05-06 10:43:07.791 testcoredata[988:c07] coredata: annotation: total fetch execution time: 0.0018s 1 rows. 2013-05-06 10:43:07.792 testcoredata[988:c07] coredata: annotation: fault fulfilled database : 0x898a030 <x-coredata://cad35d43-f6b2-4463-b59b-c9a3cd488935/person/p51846> 

from above output message, can find unmodeled property caused instead of select t0.z_ent, t0.z_pk, t0.znid, select of properties sentence generated , fault fired!

however, have read messages unmodeled property(here link):

because unmodeled properties attributes of custom nsmanagedobject subclass , not entity, fault objects know nothing them. fault objects initialized data model keys respond must in data model. means faults not reliably respond request unmodeled properties.

why did strange thing happen?

thanks in advance.


@anonymous,

choice 1): app crashed , debug output showed:

2013-05-06 14:03:05.665 testcoredata[1794:c07] -[person strtempname]: unrecognized selector sent instance 0x6ba77b0 2013-05-06 14:03:30.395 testcoredata[1794:c07] *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[person strtempname]: unrecognized selector sent instance 0x6ba77b0' 

choice 2): @ first, methods willaccessvalueforkey etc send kvo change notification, comments in nsmanagedobject.h:

- (void)willaccessvalueforkey:(nsstring *)key;      // read notification - (void)didaccessvalueforkey:(nsstring *)key;       // read notification (together willaccessvalueforkey used maintain inverse relationships, fire faults, etc.)  

secondly, following choice 2) code, strange thing(to fire faults) still alive in app.

also reply.

there things going wrong:

first: correct, @dynamic wrong. accessors in code. there nothing dynamic.

second: primitive accessors modeled properties. have "ivar property". use way use anywhere else. (but add …access… , …change… methods.)

after changing that, re-check, whether faults fired.

+++ in documenation of -didaccessvalueforkey: (nsmanagedobject) find sample correct implementation:

- (nsstring *)firstname {     [self willaccessvalueforkey:@"firstname"];     nsstring *rtn = firstname;     [self didaccessvalueforkey:@"firstname"];     return rtn; } 

Comments