问题描述
我已包含处理所有保存功能的功能。
I've included the function that is handling all of the save functionality.
这是我的问题。
我正在抓取5个输入值并将其保存为CoreData日志实体。
I am grabbing 5 input values and saving it as a CoreData Log Entity.
即使是Log对象无法验证,当我退出表单并查看表视图时仍会保存它。
Even when the Log object fails to validate, it is still being saved when I back out of the form and look at the table view.
如何强制Core Data仅保存一次对象
How can I force Core Data to only save the object once it's validated?
-(void) saveLog {
NSManagedObjectContext *managedObjectContext = [(AppDelegate_Shared *)[[UIApplication sharedApplication] delegate] managedObjectContext];
FormPickerCell *bloodPressure = (FormPickerCell *) [self.formController fieldAsObject:@"bloodpressure"];
NSInteger systolic = [(PressureDataSource*)bloodPressure.pickerCellDelegate selectedSystolicPressureForFormPickerCell:bloodPressure];
NSInteger diastolic = [(PressureDataSource*)bloodPressure.pickerCellDelegate selectedDiastolicPressureForFormPickerCell:bloodPressure];
NSLog(@"bp is %d / %d", systolic, diastolic);
NSLog(@"date is %@", [self.formController valueForField:@"date"]);
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss ZZZ"];
if (self.isNewLog && !self.validationHasFailed) {
self.log = [NSEntityDescription
insertNewObjectForEntityForName:@"Log" inManagedObjectContext:managedObjectContext];
}
NSString *heartRate = [[self.formController valueForField:@"heartrate"] stringByReplacingOccurrencesOfString:@" bpm" withString:@""];
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
self.log.created = [NSDate date];
self.log.notes = [self.formController valueForField:@"notes"];
self.log.systolic = [NSNumber numberWithInteger:systolic];
self.log.diastolic = [NSNumber numberWithInteger:diastolic];
self.log.stressLevel = [self.formController valueForField:@"stresslevel"];
self.log.logDate = [dateFormatter dateFromString:[self.formController valueForField:@"date"]];
self.log.heartrate = [f numberFromString:heartRate];
NSLog(@"Log date is %@",[self.formController valueForField:@"date"]);
[f release];
NSError *error;
NSString *title;
NSString *growlDescription;
if ([self.log validateForInsert:&error]){
NSLog(@"after validation returned true");
if(![managedObjectContext save:&error]) {
NSLog(@"Unresolved error");
title = @"Error Occurred";
growlDescription = [error localizedDescription];
self.validationHasFailed = YES;
} else {
title = @"Log Saved!";
growlDescription = @"Log saved successfully";
[self.navigationController popViewControllerAnimated:YES];
}
} else {
NSLog(@"after validation returned false");
NSLog(@"Unresolved error");
title = @"Error Occurred";
growlDescription = [error localizedDescription];
self.validationHasFailed = YES;
}
IZGrowlNotification *notification = [[IZGrowlNotification alloc] initWithTitle:title
description:growlDescription
image:nil
context:nil
delegate:self];
[[IZGrowlManager sharedManager] postNotification:notification];
[notification release];
error = nil;
}
推荐答案
这有点晚了,但我刚刚看到了您的问题,以为我会向您扔一个答案。下次保存时,将添加添加到托管对象上下文中的所有对象。您可以按原样保留代码,而只需使用 [managedObjectContext deleteObject:self.log]
删除新对象,但是下面是一个更好的方法。
This is a bit late but I just saw your question so figured I'd toss an answer at you. Any object you add to the managed object context will be saved whenever you next save. You could leave your code as is and just delete the new object with [managedObjectContext deleteObject:self.log]
but a better method is below.
您的代码:
self.log = [NSEntityDescription insertNewObjectForEntityForName:@"Log" inManagedObjectContext:managedObjectContext];
创建一个新的 Log
实例并将其插入被管理对象的上下文。相反,您要执行的操作是:
Creates a new Log
instance and inserts into the managed object context. What you want to do instead is:
self.log = [[Log alloc] initWithEntity:[NSEntityDescription entityForName:@"Log" inManagedObjectContext:managedObjectContext] insertIntoManagedObjectContext:nil];
这将创建一个尚未插入到MOC中的新 Log实例。如果验证成功,则在保存MOC之前,按如下方式插入self.log:
This will create a new 'Log' instance that has not yet been inserted into the MOC. If validation succeeds, before you save the MOC you insert the self.log as follows:
[managedObjectContext insertObject:self.log];
然后保存。如果验证失败,请不要插入对象,这样就可以了。
Then you save. If validation fails, don't insert the object and you're good to go.
这篇关于即使验证失败,核心数据也会保存对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!