ios - KVO or custom accessor methods? -
i got few entities for ios app linked relations. make simple example, lets assume got relation (one-to-many):
company <--->> person
i'm using xcode 4.6 , core data modelling tool model generation, end
//company.h @class person; @interface company : nsmanagedobject @property (nonatomic, retain) nsstring * company_name; @property (nonatomic, retain) nsnumber *has_changed; @property (nonatomic, retain) nsset *person; @end @interface company (coredatageneratedaccessors) - (void)addpersonobject:(person *)value; - (void)removepersonobject:(person *)value; - (void)addperson:(nsset *)values; - (void)removeperson:(nsset *)values; @end //company.m #import "company.h" #import "person.h" @implementation company @dynamic company_name; @dynamic has_changed; @dynamic person; @end
and
//person.h @class company; @interface person : nsmanagedobject @property (nonatomic, retain) nsstring * first_name; @property (nonatomic, retain) nsstring * last_name; @property (nonatomic, retain) company *company; @end //person.m #import "person.h" #import "company.h" @implementation person @dynamic first_name; @dynamic last_name; @dynamic company; @end
now, suppose want set boolean (implemented nsnumber in core data) attribute has_changed true
every time 1 of following occurs:
- a attribute of company entity changed, except has_changed attribute of course (since cause loop)
- a attribute of person entity changed
what best (easy implement + fast process) way implement this? able find out, there 2 options:
however, find related topic seems outdated because of changes in objective-c 2.0, core-data / cocoa or ios. example, automatically generating accessor methods using core-data modelling editor dosn't seem work when using xcode 4.6 arc enabled, since pasted @dynamic lines core-data generates anyways (see code example above).
also find bit confusing documentation says. what best way implement this? kvo? custom accessors? bit of both or compleatly different? , how possible implementation given example?
you such:
@implementation company // ... - (void) didchangevalueforkey: (nsstring *) key { [super didchangevalueforkey: key]; if (! [key isequaltostring: @"has_changed"]) self.has_changed = yes; } // ... @end
and similar person
though company
property. you'd want implement didchangevalueforkey:withsetmutation:usingobjects:
note suggestion not address common need of having controller know change. there other ways that.