ios - Detect change in NSManagedObjectContext in app delegate from different class? -


i have nsmanagedobjectcontext in app delegate , data loaded , saved fine. call nsmanagedobjectcontext master view controller , load data there. have problem when new data saved nsmanagedobjectcontext don't know how detect change , reload table in master view controller. guessing use nsnotification possible use nsnotification in master view controller nsmanagedobjectcontext in app delegate?

i tried implement code context nsmanagedobjectcontext app delegate (this won't work obviously). suggestions on how detect change in data?

[nsnotificationcenter defaultcenter]      addobserver:self         selector:@selector(handledidsavenotification:)             name:nsmanagedobjectcontextdidsavenotification           object:context];      if (![context save:&error]) {          nslog(@"there change");     } 

firstly, view controller should using managed object context created app delegate (for main thread). if app delegate doing significant data loading should create managed object context purpose , on background thread.

if can, view controller should use nsfetchedresultscontroller source data managed object context. provides easy mechanism getting data required displayed , (if make view controller frc delegate) monitor changes managed object context , tell when table view needs reloaded.

if can't use nsfetchedresultscontroller, notification method should work fine. code show above add self (which i'm assuming view controller) observer. should when view loaded , handledidsavenotification: called when need reload table view. remember remove observer when view unloaded , when view controller deallocated.

from comment, say:

i use nsfetchedresultscontroller , data loaded array populates table

don't that... use nsfetchedresultscontroller data, don't copy array - destroys of benefit of nsfetchedresultscontroller. instead:

  1. make delegate of nsfetchedresultscontroller , in controllerdidchangecontent: need [self.tableview reloaddata];.

  2. in table view delegate method tableview:cellforrowatindexpath:, use nsfetchedresultscontroller object data displayed on cell using objectatindexpath:.

please read ray wenderlich nsfetchedresultscontroller guide here.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -