Running into error when updating core data model object (iOS) -
i running weird error when try , update core data object (null error). first time save object (restaurant) saves perfectly. however, when try , update restaurant - running errors:
restaurantdetailviewcontroller.h :
@class restaurant; @interface restaurantdetailviewcontroller : uitableviewcontroller <uinavigationcontrollerdelegate, uitextviewdelegate, cuisinepickerviewcontrollerdelegate> @property (nonatomic, strong) iboutlet uitextview *reviewtextview; @property (nonatomic, strong) iboutlet uilabel *cuisinelabel; @property (nonatomic, strong) iboutlet uilabel *datelabel; @property (strong, nonatomic) iboutlet uilabel *addresslabel; @property (nonatomic, copy) nsstring *name; @property (nonatomic, copy) nsstring *address; @property (nonatomic, assign) double longitude; @property (nonatomic, assign) double latitude; @property (nonatomic, assign) cllocationcoordinate2d coordinate; @property (nonatomic, strong) nsmanagedobjectcontext *managedobjectcontext; @property (nonatomic, strong) restaurant *restauranttoedit; - (ibaction)done:(id)sender; - (ibaction)cancel:(id)sender; @end
restaurantdetailviewcontroller.m:
- (void)viewdidload { [super viewdidload]; if (self.restauranttoedit != nil) { self.navigationitem.title = @"edit restaurant"; self.navigationitem.rightbarbuttonitem = [[uibarbuttonitem alloc] initwithbarbuttonsystemitem:uibarbuttonsystemitemdone target:self action:@selector(done:)]; } date = [nsdate date]; self.navigationitem.title = name; self.reviewtextview.text = reviewtext; self.cuisinelabel.text = @"no cuisine selected"; self.addresslabel.text = address; self.datelabel.text = [self formatdate:date]; - (ibaction)done:(id)sender { restaurant *restaurant = nil; if (self.restauranttoedit != nil) { restaurant = self.restauranttoedit; } else { restaurant = [nsentitydescription insertnewobjectforentityforname:@"restaurant" inmanagedobjectcontext:self.managedobjectcontext]; } restaurant.restaurantname = name; restaurant.restaurantreview = reviewtext; restaurant.cuisine = cuisinetype; restaurant.latitude = [nsnumber numberwithdouble:self.latitude]; restaurant.longitude = [nsnumber numberwithdouble:self.longitude]; restaurant.date = date; restaurant.address = address; nserror *error; if (![self.managedobjectcontext save:&error]) { nslog(@"error %@", error); abort(); return; } [self performselector:@selector(closescreen) withobject:nil afterdelay:0.6]; } - (void)setrestauranttoedit:(restaurant *)newrestauranttoedit { if (restauranttoedit != newrestauranttoedit) { restauranttoedit = newrestauranttoedit; reviewtext = restauranttoedit.restaurantreview; cuisinetype = restauranttoedit.cuisine; date = restauranttoedit.date; address = restauranttoedit.address; } }
relevant code in mapviewcontroller.m :
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if ([segue.identifier isequaltostring:@"editrestaurant"]) { uinavigationcontroller *navigationcontroller = segue.destinationviewcontroller; restaurantdetailviewcontroller *controller = (restaurantdetailviewcontroller *)navigationcontroller.topviewcontroller; if (newrestaurant == yes) { controller.name = restaurantname; controller.address = restaurantaddress; controller.longitude = restaurantlongitude; controller.latitude = restaurantlatitude; controller.managedobjectcontext = self.managedobjectcontext; } else { restaurant *restaurant = [restaurants objectatindex:((uibutton *)sender).tag]; controller.restauranttoedit = restaurant; } } }
only thing pops in nslog (null) error. if need more code, let me know.
it appears in prepareforsegue method, aren't setting controller.managedobjectcontext property if it's update. need add
controller.managedobjectcontext = self.managedobjectcontext;
so that's it's called whether new restaurant or update.
Comments
Post a Comment