core data - iOS/coredata/swipe to delete: Why is following code not working? -
i trying swipe delete work core data using following code not working. realize there 2 approaches in following when 1 needed can't work in case. appreciate if can spot wrong. in advance suggestions.
//enables swipe delete - (void)tableview:(uitableview *)tableview commiteditingstyle:(uitableviewcelleditingstyle)editingstyle forrowatindexpath:(nsindexpath *)indexpath { // remove row data model //first approach if (editingstyle == uitableviewcelleditingstyledelete) { // delete object manageobjectcontext nslog(@"should deleting here");//this shows in log [self.managedobjectcontext deleteobject:[self.fetchedresultscontroller objectatindexpath:indexpath]]; nserror *error = nil; if (![self.managedobjectcontext save:&error]) { // handle error } } //second approach // delete item items *itemtodelete = [self.fetchedresultscontroller objectatindexpath:indexpath]; nslog(@"deleting (%@)", itemtodelete.name);//this shows in log [self.managedobjectcontext deleteobject:itemtodelete]; [[self tableview] reloaddata]; }
your 2 approaches same thing (the difference being cast), won't you.
the preferred way implement nsfetchedresultscontroller
delegate. when delete object context, delegate method controller:didchangeobject:...
called. standard implementation (xcode template: master-detail, check core data) delete row expected (via deleterowsatindexpaths:
).
if don't use delegate methods (not recommended), have call deleterowsatindexpaths
in commiteditingstyle
.
Comments
Post a Comment