ios - Newly created NSManagedObject returns temporary objectID even after save -


very simple situation. not sure why it's causing issue.

i have view creates new nsmanagedobject in child nsmanagedobjectcontext. when user presses "done", saves child context, saves parent context, posts notification newly created object's objectid. in main view controller, respond notification , try newly created object existingobjectwithid:error:.

problem fails because objectid temporary (i "cocoa error 133000"). saves 2 contexts flawless: when reload app, can see entries created. @ time need reference new object, fails.

why give me temporary object id after i've saved it?

note: have tried using obtainpermanentidsforobjects:error:, works, permanent id still fails when try use obtain object.

here's code:

-(ibaction)done:(id)sender {     if ([editordonenotification isequaltostring:knotification_objectadded]) {         // save temporary moc         nserror* e;         if (![self.tempcontext save:&e]) { // successful save             nslog(@"failed save temporary managed object context: %@", [e localizeddescription]);             [[[uialertview alloc] initwithtitle:@"database error"                                     message:@"failed add object."                                    delegate:nil                           cancelbuttontitle:@"ok"                           otherbuttontitles:nil] show];         }     }     nserror* e;     if (![[[amdatamodel shareddatamodel] maincontext] save:&e]) { // successful         nslog(@"failed save main managed object context: %@", [e localizeddescription]);         [[[uialertview alloc] initwithtitle:@"database error"                                 message:@"failed edit object."                                delegate:nil                       cancelbuttontitle:@"ok"                       otherbuttontitles:nil] show];     }     else         [[nsnotificationcenter defaultcenter] postnotificationname:editordonenotification object:[self.editingobject objectid]];       [self.navigationcontroller dismissviewcontrolleranimated:yes completion:nil]; } 

and how respond notifications:

-(void)objectadded:(nsnotification*)notification {     if (self.popovercontroller && [self.popovercontroller ispopovervisible]) {         [self.popovercontroller dismisspopoveranimated:yes];     }      nsmanagedobjectid* newobjectid = (nsmanagedobjectid*)(notification.object);     nserror* error;     amobject* object = (amobject*)[[[amdatamodel shareddatamodel] maincontext] existingobjectwithid:newobjectid error:&error]; // cocoa error 133000 happens     if (error != nil) {         nslog(@"error: not load new object in main managed object context.");     }      gmsmarker* m = [[gmsmarker alloc] init];     m.position = cllocationcoordinate2dmake(object.latitudevalue, object.longitudevalue);     m.userdata = object;     m.map = self.mapview;     [self.markers addobject:m]; }  -(void)objectedited:(nsnotification *)notification {       nsmanagedobjectid* editedobjectid = (nsmanagedobjectid*)notification.object;     nserror* error = nil;     amobject* object = (amobject*)[[[amdatamodel shareddatamodel] maincontext] existingobjectwithid:editedobjectid error:&error];     if (error != nil) {         nslog(@"error not load edited object in main managed object context");     }      //update ui based on edit      if ([self.popovercontroller ispopovervisible]) {         [self.popovercontroller dismisspopoveranimated:yes];         self.popovercontroller = nil;     } } 

because child not updated parent moc. parent moc update own instance of nsmanagedobject permanent id change not pushed down instance of nsmanagedobject belonging child moc.

update 1

i not use -objectid in situation. has uses not permanent uniqueid. in situation this, prefer add own uniqueid entity , fetch main context.

you listen context save notifications or use nsfetchedresultscontroller receive updates.


Comments

Popular posts from this blog

java - Run a .jar on Heroku -

java - Jtable duplicate Rows -

validation - How to pass paramaters like unix into windows batch file -