UITableView populating Core data one-to-many relationship using checkmark - CoreData: error: Serious application error

I have an app where I'm trying to select Resources (buildings, IT Systems) that a Programme relies on. The relationship between the Entities is Resource.resourceusedonprog<<--->Programme.usesresource. A slice of the data model is here:

I'm trying to use multiple checkmark selection on a UITableViewController of which resources get used by a particular programme. However, when I try to interact with the Resource cells in the table the app crashes with the error

CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. The left hand side for an ALL or ANY operator must be either an NSArray or an NSSet. with userInfo (null)

My code is as 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 the 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 to be triggered by the resource.resourceusedonprog = selectedProgramme; and resource.resourceusedonprog = NULL; events. if I comment them out I can check and uncheck the cells (but clearly, not change the state of resource.resourceusedonprog, i.e. what I'm trying to achieve).

Answers


I've found the answer, which is based on two issues. Firstly, the relationship key is an NSSet, not an object, for a many-to-many relationship. Secondly, the NSManagedObject Subclass for each entity automatically generates add and remove methods for each relationship. In the case of this one I've implemented

[resource removeResourceusedonprogObject:selectedProgramme]; and

[resource addResourceusedonprogObject:selectedProgramme];

The overall code is 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 the 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];


        }    }

Need Your Help

Set the content of PDF to center

html css pdf tcpdf

I am generating dynamic multicell with string. Like A, B, C etc. I want to center those multicell on the page. I need to make the $pdf->SetMargins() dynamic, which means it will automatically center

Unpredictable update in value of the variable

c++ character codeblocks dev-c++

I have been trying to solve this for quite long now . In the given program R is getting updated unpredictably even without touching it which can be seen in the 30th line where I print R is %d . Also

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.