我已经包含了处理所有保存功能的功能。
这是我的问题。
我抓取5个输入值并将其保存为CoreData Log Entity。
即使Log对象无法验证,当我退出表单并查看表格视图时,它仍然会被保存。
如何强制核心数据仅在验证后保存对象?
-(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;
}
答案 0 :(得分:2)
这有点晚了,但我刚看到你的问题,所以我想给你一个答案。无论何时下次保存,都会保存添加到托管对象上下文的任何对象。您可以按原样保留代码,只需使用[managedObjectContext deleteObject:self.log]
删除新对象,但下面有一个更好的方法。
您的代码:
self.log = [NSEntityDescription insertNewObjectForEntityForName:@"Log" inManagedObjectContext:managedObjectContext];
创建新的Log
实例并插入到托管对象上下文中。你想要做的是:
self.log = [[Log alloc] initWithEntity:[NSEntityDescription entityForName:@"Log" inManagedObjectContext:managedObjectContext] insertIntoManagedObjectContext:nil];
这将创建一个尚未插入MOC的新“Log”实例。如果验证成功,则在保存MOC之前插入self.log,如下所示:
[managedObjectContext insertObject:self.log];
然后你保存。如果验证失败,请不要插入对象,这样你就可以了。