ios - NSManagedContext returning old data instead new -
in app, have used double model of nsmanagedcontext. have 1 child managedobjectcontext
nsmainqueueconcurrencytype
, parent writermanagedobjectcontext
nsprivatequeueconcurrencytype
saves data persistentstorecoordinator
. able save data in database while retrieving getting old values instead new.
below code save context parent context
, persistentstorecoordinator
.
[managedobjectcontext performblock:^{ //push parent //nslog(@"pushing parent context (writer context)"); nserror * error; if([managedobjectcontext haschanges] && ![managedobjectcontext save:&error]){ nslog(@"main save error: %@", [error localizeddescription]); } //nslog(@"pushing persistentstore"); [self.writermanagedobjectcontext performblock:^{ //push parent nserror * error; if([self.writermanagedobjectcontext haschanges] && ![self.writermanagedobjectcontext save:&error]){ nslog(@"writer save error: %@", [error localizeddescription]); } }]; }];
found same issue on stack-overflow answer isn't helpful issue. knows how that?
set automaticallymergeschangesfromparent
of main context yes.
the writing context talking directly persistentstorecoordinator, there no way main context know there change. context has cache , don't access database data. before ios 10 need monitor notifications , call mergechangesfromcontextdidsavenotification
; in ios 10 automaticallymergeschangesfromparent
added makes lot easier.
Comments
Post a Comment