开发者

Save method for Core Data calls "nil" the first time - Why?

开发者 https://www.devze.com 2023-02-26 10:12 出处:网络
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\' stat

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, an开发者_如何学Cd 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消