objective c - Core Data not updating, updated model -
i have weird problem has stumped me rather!
i have core data entity have added new attributes to:
deleted - boolean deleteddate - date
i have following code, upon pressing sets both values on core data object:
- (ibaction)deletebuttonintable:(id)sender { //get id of selected item in table nsinteger selected = [self.tweettableview rowforview:sender]; //create predicate , fetch objects core data nsfetchrequest *request = [[nsfetchrequest alloc] init]; nspredicate *testfortrue = [nspredicate predicatewithformat:@"approved == no"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:@"postdate" ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setpredicate:testfortrue]; [request setsortdescriptors:sortdescriptors]; [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; //setup request [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; //assign predicate fetch request nserror *error = nil; //create array returned objects nsarray *fetchedobjects = [_managedobjectcontext executefetchrequest:request error:&error]; tweet *selectedtweet = [fetchedobjects objectatindex:selected]; if (selectedtweet) { selectedtweet.deleted = [nsnumber numberwithbool:true]; selectedtweet.deleteddate = [nsdate date]; nslog(@"%@",selectedtweet); [self refreshtableview]; if (! self.tweettableview){ nslog(@"tableview doesn't exist!!)"); } [[self tweettableview] reloaddata]; [[self managedobjectcontext] commitediting]; [self saveaction:nil]; } if ([self.autowritetweets isequaltostring:@"yes"]){ [self writetweetstoxml]; [self saveaction:nil]; } }
now, if watch object in xcode breaks, can see attribute change on object pass through function, have table displaying datasource, filtered show objects have deleted bool set true, , nothing ever shows there.
now, make things more confusing have function exports array of objects:
-(void)writetweetstoxml{ //create new fetch request nsfetchrequest *request = [[nsfetchrequest alloc] init]; //set new predicate fetch tweets have been favourited nspredicate *filterfavourite = [nspredicate predicatewithformat:@"approved == yes"]; nssortdescriptor *sortdescriptor1 = [[nssortdescriptor alloc] initwithkey:self.exportsort ascending:no]; nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortdescriptor1, nil]; [request setsortdescriptors:sortdescriptors]; //setup request [request setentity:[nsentitydescription entityforname:@"tweet" inmanagedobjectcontext:_managedobjectcontext]]; [request setresulttype:nsdictionaryresulttype]; //assign predicate fetch request [request setpredicate:filterfavourite]; nserror *error = nil; //create array returned objects nsarray *tweetstoexport = [_managedobjectcontext executefetchrequest:request error:&error]; nsassert2(tweetstoexport != nil && error == nil, @"error fetching events: %@\n%@", [error localizeddescription], [error userinfo]); //nsstring *documents = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes) objectatindex:0]; //nsstring *path = [nsstring stringwithformat:@"%@/tweets.xml", documents]; nsstring *writeerror; if(tweetstoexport) { nsstring * exportlocationfull = [[nsstring alloc]initwithformat:@"%@/tweets.xml",self.exportlocation]; bool success = [tweetstoexport writetofile:exportlocationfull atomically:yes]; nslog(@"write status = %d %@", success, exportlocationfull); } else { nslog(@"%@",writeerror); } }
now, when @ exported file, 2 things happen odd!
firstly, object have seen have it's deleted value set true, exports value 0.
secondly, deleteddate attribute not export @ all, every, despite being in core data model. can't see way can happen doing no specific filtering on export.
it's getter/setter somewhere broken, have checked class files , should , set @dynamic.
any appreciated i'm bit lost hell going on.
people had warned me core data's quirks, plain odd!
cheers
gareth
note 1
as aside, using exact same code first section set other attributes on objects filtered , seems work fine!
you should not name core data attribute "deleted", conflicts isdeleted
method of nsmanagedobject
.
compare https://stackoverflow.com/a/16003894/1187415 short analysis of problem.
there other attribute names cause conflicts, e.g. "updated" (compare cannot use predicate compares dates in magical record). unfortunately, there no warnings @ compile time or runtime, , documentation on acceptable attribute names quite vague.
Comments
Post a Comment