Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5961519
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:58:33+00:00 2026-05-22T18:58:33+00:00

I’m implementing a car manager for an iPhone app and I have trouble saving

  • 0

I’m implementing a car manager for an iPhone app and I have trouble saving a new car. So I have a “Car” entity (and per-loaded DB), containing multiples attributes. I have 2 booleans “saved” and “selected” to track which car the user added to his list (if added, saved = 1) and which car is currently selected. So when I create a new car, I deselect the old one (selected=0), and want to modify the new car to set its attributes saved=1 and selected=1.

Here is 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 log all the time: “error saving car.” on both functions. So there is definitely something wrong.

Also, it’s pretty anoying to fetch the car I want to modify it, instead of doing right away an update. If there another please tell me!

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T18:58:34+00:00Added an answer on May 22, 2026 at 6:58 pm

    In your header file, you should set up a mutable array for your cars.

    NSMutableArray *carArray;
    

    and

    @property (nonatomic, retain) NSMutableArray *carArray;
    

    Then make sure to synthesize it in your implementation file. Then when you fetch from your managed object context, you can set your array with the contents returned

    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];
    }
    

    Doing this will hold onto all the objects in your managed object context, so when you want to modify a Car, you can find it there instead of fetching again. If you need to sort it, you can apply a sort descriptor initialized with one of your attributes as a key. For example:

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

    This will sort the Cars based on whether they are selected or not.

    As for why you’re getting an error, that could be one of many reasons. Typically the MOC will fail to save if one of your attributes is set to nil, so that might be the cause. You can get some detail from it if you set something like the following up

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

    This will return the actual error you ran into. It would also be a good idea to set up a log to make sure you have Car specified. Something like NSLog(@"selected car %@", carToSave); just to be safe

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.