选择新插入的Core Data对象会导致崩溃

时间:2016-10-28 19:55:47

标签: ios objective-c core-data tableview didselectrowatindexpath

我有一个tableview,列出了越野队的跑步者。如果您选择跑步者,他们所有的比赛和时间都将以新的桌面视图显示。

但是,在添加跑步者之后,如果选择了该跑步者,应用程序会在应该打开一个空的桌面视图时崩溃。在这里我创建一个新的跑步者,将其添加到数据源数组,将其添加到Core Data,并更新我的tableview:

[self presentViewController:picker animated:YES completion:^{
    //For iOS 10 and higher versions so it can set the proper flashmode
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 10) {
       [picker setCameraFlashMode:UIImagePickerControllerCameraFlashModeOn];
    }
}];

这是我的didSelectRowAtIndexPath方法:

Runner *newRunner = [[Runner alloc] init];
//I set runner properties here.

[allRunners insertObject:newRunner atIndex:0];
//allRunners is used to populate the tableView.

NSManagedObject *savedRunner = [NSEntityDescription insertNewObjectForEntityForName:@"Runner" inManagedObjectContext:context];
//I set attributes here.

[appDelegate saveContext];

[self.myTableView insertRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationLeft];

如果我先退出视图,然后回到它,我可以选择新创建的跑步者。这是怎么回事?这是错误:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    RacesOfRunner *vc = [self.navigationController.storyboard instantiateViewControllerWithIdentifier:@"RacesOfRunner"];

    NSManagedObject *selectedRunner = [selectedRunners objectAtIndex:indexPath.row];

    NSArray *results = [GlobalFunctions getData:@"Result" pred:@"resultToRunner = %@" predArray:[NSArray arrayWithObjects: selectedRunner, nil] context:context];
    //executes fetch request and returns the results.

    vc.results = [results mutableCopy];
    vc.name = [selectedRunner valueForKey:@"name"];

    [self.navigationController pushViewController:vc animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:false];

}

导致崩溃的特定行是我的getData方法中的executeFetchRequest:

Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[Runner longLongValue]: unrecognized selector sent to instance 0x1702d2600'

0 个答案:

没有答案