简体   繁体   中英

Core Data relationships issue

I've got a simple app with three views HomeViewController , AddViewController , and AddCategoryViewController .

My data model has two Entities, with relationships like this:

在此处输入图片说明

My goal is to be able to display WMDGActivity objects in the tableview in HomeViewController , grouped under sections defined by WMDGCategory objects.

New WMDGCategory objects are added via a textfield in AddCategoryViewController and new WMDGActivity objects are added via a textfield in AddViewController .

My problem is that the app crashes upon canceling or saving in either of the auxiliary views. The error code is invariably:

reason: '[<WMDGActivity 0x8a54830> valueForUndefinedKey:]: the entity WMDGActivity is not key value coding-compliant for the key "WMDGCategory".'

I also see this every time I roll the picker wheel containing the names of WMDGCategory objects:

data: {
    activities = "<relationship fault: 0x8db1600 'activities'>";
    name = Pastimes;

Indeed, another mystery is that the app is apparently saving these objects before the crash. I've tried stepping through the code, but haven't come up with a solution yet. I suspect there is a problem with the way my entities are set up, or with my code that interacts with them.

Here is what I believe to be the relevant code:

From HomeViewController (delegate for both AddViewController , and AddCategoryViewController :

#pragma mark AddViewControllerDelegate stuff

-(void) addViewControllerDidSave
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [localContext MR_saveToPersistentStoreAndWait];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

-(void) addViewControllerDidCancel:(WMDGActivity *) activityToDelete
{
    [activityToDelete MR_deleteEntity];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

#pragma mark AddCatControllerDelegate stuff

-(void) addCatControllerDidSave
{
    NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
    [localContext MR_saveToPersistentStoreAndWait];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

-(void) addCatControllerDidCancel:(WMDGCategory *) categoryToDelete
{
    [categoryToDelete MR_deleteEntity];
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    [self refreshData];

}

From AddCategoryViewController:

- (IBAction)saveButton:(UIBarButtonItem *)sender
{
    if (self.catTextField.text.length > 0)
    {
        self.thisCategory.name = self.catTextField.text;
    }
    [self.delegate addCatControllerDidSave];
}

- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
    [self.delegate addCatControllerDidCancel:self.thisCategory];
}

And from AddViewController:

- (IBAction)saveButton:(UIBarButtonItem *)sender
{
    if (self.activityField.text.length > 0)
    {

        if (self.categoryLabel.text.length < 1)
        {
            self.thisCategory.name = @"Uncategorized";
            //            self.thisActivity.activityName = self.activityField.text;
            //            [self.delegate addActivityViewControllerDidSave];
        }

        else
        {
        self.thisCategory.name = self.categoryLabel.text;
        self.thisActivity.name = self.activityField.text;
        NSLog(@"Category name is %@", self.thisCategory.name);
        NSLog(@"Activity name is %@", self.thisActivity.name);
        }


        [self.delegate addViewControllerDidSave];

    }

    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No activity entered"
                                                        message:@"Please enter a new activity or Cancel"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
        [alert show];
    }



}

- (IBAction)cancelButton:(UIBarButtonItem *)sender
{
    [self.delegate addViewControllerDidCancel:self.thisActivity];
}

Can anyone please show me what I'm doing wrong?

My problem is that the app crashes upon canceling or saving in either of the auxiliary views. The error code is invariably:

It would help to see the error call stack and the code where the error occurred. But I'll go ahead and throw out a guess, which is that you're configuring an NSFetchedResultsController and specifying WMDGCategory as the sectionNameKeyPath . There are two things wrong with this. First, the key path is toCategory . WMDGCategory is the data type. Second, to organize your table view by category name, you would need to use the key path toCategory.name . Also, you're going to need to pre-sort your activities by toCategory.name (a requirement of NSFetchedResultsController ).

I also see this every time I roll the picker wheel containing the names of WMDGCategory objects:

What you're seeing is the description of the WMDCategory object. What you need is the value of the name property. You need to use [category valueForKey:@"name"] , or if you're using convenience classes, category.name .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM