objective c - didChangeObject: not called of NSFetchedResultsController -


i have tried cannot figure out wrong. have nsfetchedresultscontroller , fetch posts out of core data. have method insert new posts same context , save context. normally, didchangeobject: method should called now, not. have similar view controller same on different table (=nsmanaged object) , there didchangeobject: method gets called successfully.

what have tried far:

  • created context view controller

  • removed cache of nsfetchedresultscontroller

the following method fetches data out of core data db , 'stores' nsfetchedresultscontroller (variable: controller).

//reloading newsgroups coredata - (void)reloadnewsgroupsfromcoredata {         //creating fetch request     nsfetchrequest *fetchrequest = [[nsfetchrequest alloc] initwithentityname:@"post"];      //sorting transient date (hour , secons cutted off)     nssortdescriptor *sortbydate = [[nssortdescriptor alloc] initwithkey:@"datecreatedtransient" ascending:no];     nsarray *sortdescriptors = [[nsarray alloc] initwithobjects:sortbydate, nil];     [fetchrequest setsortdescriptors:sortdescriptors];      //where clause     [fetchrequest setpredicate:[nspredicate predicatewithformat:@"ref_subscribed_newsgroup == %@ , references == nil", self.newsgroup]];      //setting fetchrequest     self.controller = [[nsfetchedresultscontroller alloc]                        initwithfetchrequest:fetchrequest                        managedobjectcontext:self.context                        sectionnamekeypath:@"datecreatedforsections"                        cachename:nil]; //todo, maybe add cache here!      //fetch     nserror *error;     [self.controller performfetch:&error];      if (error) {         nslog(@"%@", error);     } } 

furthermore, according documentation set delegate of nsfetchedresultscontroller view controller (this same aforementioned fetch method located).

in viewdidload:

self.controller.delegate = self; 

then have implemented methods protocol, didchangeobject:

- (void)controller:(nsfetchedresultscontroller *)controller didchangeobject:(id)anobject atindexpath:(nsindexpath *)indexpath forchangetype:(nsfetchedresultschangetype)type newindexpath:(nsindexpath *)newindexpath {         uitableview *tableview = self.tableview;       // have added breakpoint here never stops here.      switch(type) {          case nsfetchedresultschangeinsert:             [tableview insertrowsatindexpaths:[nsarray arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade];              break;         case nsfetchedresultschangedelete:             [tableview deleterowsatindexpaths:[nsarray arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];             break;          case nsfetchedresultschangeupdate:             [self configurecell:[tableview cellforrowatindexpath:indexpath]                     atindexpath:indexpath];              break;          case nsfetchedresultschangemove:             [tableview deleterowsatindexpaths:[nsarray                                                arraywithobject:indexpath] withrowanimation:uitableviewrowanimationfade];             [tableview insertrowsatindexpaths:[nsarray                                                arraywithobject:newindexpath] withrowanimation:uitableviewrowanimationfade];             break;     }  } 

then have method stores posts core data. think important: method invoked via delegate thread. (the server connection invokes using performselectoronmainthread:...)

- (void)storeintocoredata:(nsarray*)postsarray {  ...         post *entity = [nsentitydescription insertnewobjectforentityforname: @"post" inmanagedobjectcontext:context];          entity.inreply = dummy.in_reply;         entity.body = dummy.body;         entity.email = dummy.email;         entity.from = dummy.from;         entity.messageidentifier = dummy.message_identifier;         entity.references = dummy.references;         entity.subject = dummy.subject;         entity.datecreated = dummy.date;         entity.unread = [nsnumber numberwithint:1]; ...     [self.context save:&error];     if (error)     {         nslog(@"error: %@", error);     }  // important: calling method again here,  // fetches posts out of core data. // ugly , not performant.  // invoke reloaddata on tableview reload entire table // there 1 insert entire reload not necessary.      [self reloadnewsgroupsfromcoredata];     [self.tableview reloaddata];  // have because didchangeobject method gets not called. } 

does have hint or suggestion wrong?

thanks , regards, chris

the problem you're setting controller delegate in viewdidload , later allocating new controller in reloadnewsgroupsfromcoredata. newly allocated controller doesn't have delegate set. try setting delegate directly in reloadnewsgroupsfromcoredata.

you typically don't want allocate new nsfetchedresultscontroller whenever changes in data. entire point of using nsfetchedresultscontroller delegate messages via nsfetchedresultscontrollerdelegate when changes. need allocate new nsfetchedresultscontroller if fetch request changes. suggest having @ apple docs.


Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -