ios - Objective C property vs ivar issues with Core Data -
i spent several hours trying figure out problem, , think comes down fundamental understanding of objective c, though manifested through working on core data. i'm not surprised - i've been working objective c , cocoa touch 2 months now.
my situation have series of models connected fine in cd. app works fine until tried extending yesterday. have main model job in view controller class property in .h file. in viewwillappear method have relationship through object, like:
/** project ivar **/ nsmanagedobject *project = [job valueforkey:@"project"]; nsarray *divisions = [[project valueforkey:@"divisions"] allobjects]; //do divisions --> crash
...
/** project property **/ project = [job valueforkey:@"project"]; nsarray *divisions = [[project valueforkey:@"divisions"] allobjects]; //do someting, ---> a-ok!
so, why app crash when try things results of [project valueforkey:] unless make project class property?
edit
it appears including if(!divisions)
conditional in there (which should null when view first loads), doesn't statements provided above , produces exc_bad_access
. however, when leaving out, code works fine.
- (void)viewwillappear:(bool)animated { [super viewwillappear:animated]; if(!divisions){ nsmanagedobject *project = [[job valueforkey:@"project"] retain]; nsarray *divs = [[project valueforkey:@"divisions"] allobjects]; nssortdescriptor *alphasort = [nssortdescriptor sortdescriptorwithkey:@"order" ascending:yes]; divisions = [[divs sortedarrayusingdescriptors:[nsarray arraywithobject:alphasort]] mutablecopy]; } [[self tableview] reloaddata]; }
i'll accept there's bigger memory management problem going on. should go , re-read book chapters on obj-c , retrace variables makes sense?
i can't tell based on limited code provided simplest explaination crash don't check if divs
or divisions
arrays empty before performing operations on them.
if project.divisions
relationship empty this:
nsarray *divs = [[project valueforkey:@"divisions"] allobjects];
... return empty array. attempt address element of array e.g. [divs objectatindex:0]
produce error.
also, divisions
appears ivar of controller object don't use either preferred self.divisions
reference form nor [divisions retain]
when assigning 'divs' array it. since divs
array returned autoreleased, purged when pool next drains , array in divisions
disappear if not empty.
simply change divisions
self.divisions
fix.
Comments
Post a Comment