i update core data in background thread, so:
entry.message = [self contentfornotewithedml:note.content]; entry.datalastmodified = [nsdate date]; [entry.managedobjectcontext save:nil]; dispatch_async(dispatch_get_main_queue(), ^{ nserror *error; if (![[self fetchedresultscontroller] performfetch:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); } [self.tableview reloaddata]; });
on each cell of tableview, displays different entry fetchedresultscontroller
. on main thread, nslog
in cellforrowatindexpath
on datalastmodified
date, , date doesn't change recent value. if close app , run again, updates contents of cell , datalastmodified
date changes correct value.
it seems changing data, required, tableview
isn't seeing changes until app restarted. ideas why?
edit: doing nslog
in cellforrowatindexpath
on background thread gives the correct data, doing on main thread not.
edit 2: how background context works:
nsnotificationcenter *notificationcenter = [nsnotificationcenter defaultcenter]; [notificationcenter addobserver:[appdelegate applicationdelegate].coredatamanager selector:@selector(mergechanges:) name:nsmanagedobjectcontextdidsavenotification object:[appdelegate applicationdelegate].coredatamanager.managedobjectcontext]; nspersistentstorecoordinator *journaldatapsc = [appdelegate applicationdelegate].coredatamanager.managedobjectcontext.persistentstorecoordinator; dispatch_queue_t addorupdateentriesqueue = dispatch_queue_create("com.app.addorupdateentries", null); dispatch_async(addorupdateentriesqueue, ^{ nsmanagedobjectcontext *journaldatamoc = [[nsmanagedobjectcontext alloc] init]; [journaldatamoc setpersistentstorecoordinator:journaldatapsc]; //some code me entry on context entry.message = [self contentfornotewithedml:note.content]; entry.datalastmodified = [nsdate date]; [entry.managedobjectcontext save:nil]; [[nsnotificationcenter defaultcenter] removeobserver:[appdelegate applicationdelegate].coredatamanager]; dispatch_async(dispatch_get_main_queue(), ^{ nserror *error; if (![[self fetchedresultscontroller] performfetch:&error]) { nslog(@"unresolved error %@, %@", error, [error userinfo]); } [self.tableview reloaddata]; }); }); dispatch_release(addorupdateentriesqueue);
make sure you're using moc's correctly, not thread-safe , can used in thread created in. in case, if you're doing right enter.managedobjectcontext
different moc of fetched results controller (which in main thread).
that means saves in background not propagated main thread moc. make sure handle nsmanagedobjectcontextdidsavenotification
in main thread adding observer when create fetched results controller.
looking @ code here few points notice:
- you should register save notification thrown background moc not main thread moc.
- you should initialize background moc
initwithconcurrencytype:
usingnsprivatequeueconcurrencytype
- once you're using
nsprivatequeueconcurrencytype
it's better useperformblock:
make changes , save instead of using low-level gcd dispatch methods.
Comments
Post a Comment