核心数据的保存方法调用“nil”第一次——为什么?

发布于 2024-11-02 05:20:57 字数 3060 浏览 0 评论 0原文

我目前正在开发一个核心数据调查应用程序。现在,在程序首次创建核心数据实体后保存它时,我的程序遇到了问题。我有一个“else”语句,在我第一次保存它时被调用。值已保存,但应用程序崩溃。然后我可以重新启动该应用程序,并且 NSManagedObject 可以正常工作。问题出在我的 else 语句上,但我不知道哪里出了问题。

这是代码:

- (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];
}

非常感谢任何帮助。

谢谢,

凯文

I am currently working on a core data survey application. Right now, I am having trouble with my program when saving my core data entity after it is first created by the program. I have an 'else' statement that is called the first time I save it. The values are saved, but the application crashes. I can then relaunch the application, and that NSManagedObject works normally. The problem lies in my else statement, but I can't figure out what is wrong.

Here is the code:

- (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];
}

Any help is greatly appreciated.

Thanks,

Kevin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

山田美奈子 2024-11-09 05:20:57

由于我缺乏有关错误的详细信息,我猜测问题出在这个条件上:

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

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

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

...这永远不会起作用。

Since I lack detailed information about the error I am going to guess that the problem is with this conditional:

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

You are getting the managedObjectContext from the team object even though that line of code is called only if team==nil. That means the call is always really:

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

... which is never going to work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文