views:

4

answers:

0

I've included the function that is handling all of the save functionality.

Here's my problem.

I am grabbing 5 input values and saving it as a CoreData Log Entity.

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.

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;    




}