objective c - NSFetchedResultsController not displaying changes from background thread -
my app using nsfetchedresultscontroller tied core data store , has worked far, trying make update code asynchronous , having issues. have created nsoperation sub-class updates in , adding new object nsoperationqueue. updates code executing expect , have verified through debug logs , examining sqlite store after runs.
the problem after background operation completes, new (or updated) items not appear in uitableview. based on limited understanding, believe need notify main managedobjectcontext changes have occurred may merged in. notification firing, nut no new items appear in tableview. if stop app , restart it, objects appear in tableview, leading me believe being inserted core data store not being merged managedobjectcontext being used on main thread.
i have included sample of operation's init, main , notification methods. missing important or maybe going in wrong way? appreciated.
- (id)initwithdelegate:(appdelegate *)thedelegate { if (!(self = [super init])) return nil; delegate = thedelegate; return self; } - (void)main { [self setupdatecontext:[self managedobjectcontext]]; nsmanagedobjectcontext *mainmoc = [self newcontexttomainstore]; nsnotificationcenter *center = [nsnotificationcenter defaultcenter]; [center addobserver:self selector:@selector(contextdidsave:) name:nsmanagedobjectcontextdidsavenotification object:updatecontext]; [self setmaincontext:mainmoc]; // create/update objects maincontext. nserror *error = nil; if (![[self maincontext] save:&error]) { dlog(@"error saving event coredata store"); } dlog(@"core data context saved"); } - (void)contextdidsave:(nsnotification*)notification { dlog(@"notification fired."); sel selector = @selector(mergechangesfromcontextdidsavenotification:); [[delegate managedobjectcontext] performselectoronmainthread:selector withobject:notification waituntildone:yes]; }
while debugging, examined notification
object being sent in contextdidsave:
, seems contain of items added (excerpt below). continues make me think inserts/updates happening correctly somehow merge not being fired.
nsconcretenotification 0x6b7b0b0 {name = nsmanagingcontextdidsavechangesnotification; object = <nsmanagedobjectcontext: 0x5e8ab30>; userinfo = { inserted = "{(\n <gcteam: 0x6b77290> (entity: gcteam; id: 0xdc5ea10 <x-coredata://f4091bae-4b47-4f3a-a008-b6a35d7ab196/gcteam/p1> ; data: {\n changed =
the method receives notification must indeed notify context, can try this, doing in application:
- (void)updatetable:(nsnotification *)savenotification { if (fetchedresultscontroller == nil) { nserror *error; if (![[self fetchedresultscontroller] performfetch:&error]) { //update handle error appropriately. nslog(@"unresolved error %@, %@", error, [error userinfo]); exit(-1); // fail } } else { nsmanagedobjectcontext *context = [fetchedresultscontroller managedobjectcontext]; // merging changes causes fetched results controller update results [context mergechangesfromcontextdidsavenotification:savenotification]; // reload table view data [self.tableview reloaddata]; } }
hope helps.
Comments
Post a Comment