UITableView populating Core data one-to-many relationship using checkmark - CoreData: error: Serious application error -
i have app i'm trying select resources (buildings, systems) programme relies on. relationship between entities resource.resourceusedonprog<<--->programme.usesresource. slice of data model here:
i'm trying use multiple checkmark selection on uitableviewcontroller of resources used particular programme. however, when try interact resource cells in table app crashes error
coredata: error: serious application error. exception caught during core data change processing. bug within observer of nsmanagedobjectcontextobjectsdidchangenotification. left hand side or operator must either nsarray or nsset. userinfo (null)
my code follows:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"resource cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // configure cell... resource *resource = [self.fetchedresultscontroller objectatindexpath:indexpath]; nsstring *fullname = [nsstring stringwithformat:@"%@ %@", resource.hasmanager.firstname, resource.hasmanager.surname]; cell.textlabel.text = resource.name; cell.detailtextlabel.text = fullname; if (resource.resourceusedonprog == selectedprogramme){ cell.accessorytype = uitableviewcellaccessorycheckmark;} else {cell.accessorytype = uitableviewcellaccessorynone;} return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)path { uitableviewcell *cell = [tableview cellforrowatindexpath:path]; resource *resource = [self.fetchedresultscontroller objectatindexpath:path]; if (cell.accessorytype == uitableviewcellaccessorycheckmark) { cell.accessorytype = uitableviewcellaccessorynone; resource.resourceusedonprog = null; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; resource.resourceusedonprog = selectedprogramme; } }
the error seems triggered resource.resourceusedonprog = selectedprogramme; , resource.resourceusedonprog = null; events. if comment them out can check , uncheck cells (but clearly, not change state of resource.resourceusedonprog, i.e. i'm trying achieve).
i've found answer, based on 2 issues. firstly, relationship key nsset, not object, many-to-many relationship. secondly, nsmanagedobject subclass each entity automatically generates add , remove methods each relationship. in case of 1 i've implemented
[resource removeresourceusedonprogobject:selectedprogramme];
,
[resource addresourceusedonprogobject:selectedprogramme];
the overall code here:
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"resource cell"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) { cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; } // configure cell... resource *resource = [self.fetchedresultscontroller objectatindexpath:indexpath]; nsstring *fullname = [nsstring stringwithformat:@"%@ %@", resource.hasmanager.firstname, resource.hasmanager.surname]; cell.textlabel.text = resource.name; cell.detailtextlabel.text = fullname; if ([resource.resourceusedonprog containsobject:selectedprogramme]){ cell.accessorytype = uitableviewcellaccessorycheckmark;} else {cell.accessorytype = uitableviewcellaccessorynone;} return cell; } - (void)tableview:(uitableview *)tableview didselectrowatindexpath:(nsindexpath *)path { uitableviewcell *cell = [tableview cellforrowatindexpath:path]; resource *resource = [self.fetchedresultscontroller objectatindexpath:path]; nslog(@"selectedresource is: %@", resource.name); if (cell.accessorytype == uitableviewcellaccessorycheckmark) { cell.accessorytype = uitableviewcellaccessorynone; [resource removeresourceusedonprogobject:selectedprogramme]; } else { cell.accessorytype = uitableviewcellaccessorycheckmark; [resource addresourceusedonprogobject:selectedprogramme]; } }
Comments
Post a Comment