核心数据的保存方法调用“nil”第一次——为什么?
我目前正在开发一个核心数据调查应用程序。现在,在程序首次创建核心数据实体后保存它时,我的程序遇到了问题。我有一个“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于我缺乏有关错误的详细信息,我猜测问题出在这个条件上:
您正在从
team
对象获取 ManagedObjectContext,即使仅当时才调用该行代码团队==nil
。这意味着调用总是真的:...这永远不会起作用。
Since I lack detailed information about the error I am going to guess that the problem is with this conditional:
You are getting the managedObjectContext from the
team
object even though that line of code is called only ifteam==nil
. That means the call is always really:... which is never going to work.