Core Data的保存方法第一次调用“nil” - 为什么?

时间:2011-04-19 06:39:34

标签: iphone core-data

我目前正在研究核心数据调查应用程序。现在,在程序首次创建核心数据实体后保存核心数据实体时,我的程序出现问题。我有一个'else'语句,这是我第一次保存它时调用的。值已保存,但应用程序崩溃。然后我可以重新启动应用程序,并且NSManagedObject正常工作。问题在于我的其他陈述,但我无法弄清楚出了什么问题。

以下是代码:

- (IBAction)save:(id)sender {
  if (rootController != nil) {
    if (team != nil) {
      [team setValue:name.text forKey:@"name"]; //UITextfield -> NSString
      [team setValue:teamNumber.text forKey:@"teamNumber"]; //UITextfield -> NSString
      [team setValue:[NSNumber numberWithInt:totalRank] forKey:@"totalRankValue"]; //Int -> NSNumber
      [team setValue:driveTrain.text forKey:@"driveTrain"]; //UITextfield -> NSString
      [team setValue:[NSNumber numberWithInt:autonomousRank] forKey:@"autonomousRankValue"]; //Int -> NSNumber
      [team setValue:[NSNumber numberWithInt:robotSpeedRank] forKey:@"robotSpeedRankValue"]; //Int -> NSNumber
      [team setValue:[NSNumber numberWithInt:minibotRank] forKey:@"minibotSpeedRankValue"]; //Int -> NSNumber
      [team setValue:[NSNumber numberWithInt:grabberRank] forKey:@"grabberRankValue"]; //Int -> NSNumber

      [rootController saveContext];

      NSLog(@"Save works - team is not nil");
      //Begin debug

      NSError* error;
      if(![[team managedObjectContext] save:&error]) {
        NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
        NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
        if(detailedErrors != nil && [detailedErrors count] > 0) {
          for(NSError* detailedError in detailedErrors) {
            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
          }
        }
        NSLog(@"  %@", [error userInfo]);

      }

      //End debug
    } 
    else {
      [rootController insertTeamWithName:name.text 
                              teamNumber:teamNumber.text
                              driveTrain:driveTrain.text
                     autonomousRankValue:[NSNumber numberWithInt:autonomousRank]
                        grabberRankValue:[NSNumber numberWithInt:grabberRank]
                   minibotSpeedRankValue:[NSNumber numberWithInt:minibotRank] 
                     robotSpeedRankValue:[NSNumber numberWithInt:robotSpeedRank]
                          totalRankValue:[NSNumber numberWithInt:totalRank]]; 

      NSLog(@"Team is nil");

      //Begin debug

      NSError* error;
      if(![[team managedObjectContext] save:&error]) {
        NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
        NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
        if(detailedErrors != nil && [detailedErrors count] > 0) {
          for(NSError* detailedError in detailedErrors) {
            NSLog(@"  DetailedError: %@", [detailedError userInfo]);
          }
        }
        else {
          NSLog(@"  %@", [error userInfo]);
        }
      }

      //End debug


    }
  }
  [self dismissModalViewControllerAnimated:YES];
}

非常感谢任何帮助。

谢谢,

凯文

1 个答案:

答案 0 :(得分:1)

由于我缺少有关错误的详细信息,我猜这个问题与此条件有关:

if (team != nil) {
//...
}else{
//..
    if(![[team managedObjectContext] save:&error]) {
         //...
}

您正在从team对象获取managedObjectContext,即使仅在team==nil时调用该行代码。这意味着电话总是真的:

if(![[nil managedObjectContext] save:&error]) {

......这是永远无法奏效的。