IOS CoreData Update Object Object

I am implementing a car manager for an iPhone application, and I have problems saving a new car. So I have a "Car" object (and a loaded DB) containing multiples attributes. I have 2 Booleans "saved" and "selected" to track which car the user has added to his list (if added, saved = 1) and which car is currently selected. Therefore, when I create a new car, I deselect the old one (selected = 0) and want to change the new car to set its saved attributes = 1 and selected = 1.

Here are my functions:

- (IBAction)save
{
    // Disable the previous car selection.
    [self deselectCurrentSelectedCar];

    // Add new car as saved and selected.
    [self saveAndSelectNewCar];

    // Call the delegate to dismiss the modal view.
    [_delegate dismissAndSave];
}

- (void)deselectCurrentSelectedCar
{    
    // Fetched saved car.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];

    // Set predicate and sort orderings...
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = 1"];
    [fetchRequest setPredicate:predicate];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
        NSLog(@"[AddCarViewController] deselect car: car not found.");
    }
    else {
        // Get car and assign new selected value.
        Car *carToSave = (Car *)[mutableFetchResults objectAtIndex:0];
        [carToSave setSelected:[NSNumber numberWithInt:0]];

        // Save the car.
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {
            // Handle the error.
            NSLog(@"[AddCarViewController] deselect car: error saving car.");
        }
        else {
            NSLog(@"[AddCarViewController] deselect car: car saved.");
        }
    }

    // Memory management.
    [fetchRequest release];
    [mutableFetchResults release];
}
- (void)saveAndSelectNewCar
{
    // Get the car, and pass to the delegate the new settings.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
    [fetchRequest setEntity:entity];

    // Set predicate and sort orderings...
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(year=%@) AND (make=%@) AND (model=%@) AND (d=%@) AND (n=%@)", _car.year, _car.make, _car.model, _car.d, _car.n];
    [fetchRequest setPredicate:predicate];

    // Execute the fetch -- create a mutable copy of the result.
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle error.
        NSLog(@"[AddCarViewController] save and select: can't save the new car");
    }
    else {
        // Get the car selected.
        Car *selectedCar = (Car *)[mutableFetchResults objectAtIndex:0];
        [selectedCar setValue:[NSNumber numberWithInt:1] forKey:@"selected"];
        [selectedCar setValue:[NSNumber numberWithInt:1] forKey:@"saved"];

        // Save the car.
        NSError *error = nil;
        if (![self.managedObjectContext save:&error]) {
            // Handle the error.
            NSLog(@"[AddCarViewController] save and select: error saving car.");
        }
        else {
            NSLog(@"[AddCarViewController] save and select: car saved.");
        }

        // Add car to delegate.
        [EcoAppAppDelegate setUserCar:selectedCar];
    }

    // Memory management.
    [fetchRequest release];
    [mutableFetchResults release];
}

And I have this magazine all the time: "a bug saving a car." for both functions. So there is definitely something wrong.

, , , , , . , !

.

+3
1

.

NSMutableArray *carArray;

@property (nonatomic, retain) NSMutableArray *carArray;

, . , ,

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:[self managedObjectContext]];
[fetchRequest setEntity:entity];

// Execute the fetch -- create a mutable copy of the result.
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy];
if (mutableFetchResults == nil) {
    // Handle the error.
    NSLog(@"[AddCarViewController] deselect car: car not found.");
} else {
    [self setCarArray:mutableFetchResults];
}

, , , , . , , . :

NSSortDescriptor *sorter = [NSSortDescriptor sortDescriptorWithKey:@"selected" ascending:YES];
[carArray sortUsingDescriptors:[NSArray arrayWithObject:sorter]];

, .

, , . , MOC , nil, . , - :

if (![self.managedObjectContext save:&error]) {
    NSLog(@"failed with error %@", error);
}

, . , , Car. - NSLog(@"selected car %@", carToSave);

+3

All Articles