6
[newVehicle setValue: _txtFieldVehicleNumber.text forKey:@"number"];

[newVehicle setValue: lblFuelType.text forKey:@"fueltype"];

[newVehicle setValue: lblFuelUnit.text forKey:@"fuelunit"];

[newVehicle setValue: lblDistanceUnit.text forKey:@"distanceunit"];

I want to update my core data entity named "Vehicle", for that entity I have several attributes and I want to update some of them but not all when I select particular attribute from the entity. So what should I do ??

1
  • 2
    You fetch the instance of that entity from Core Data, update the attributes you want to, save core data context. Sep 27, 2013 at 14:35

2 Answers 2

17

you can do the following (approximate code. Implement error handling and check syntax).

NSFetchRequest *fetchRequest=[NSFetchRequest fetchRequestWithEntityName:@"Vehicle"];
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"vehicle_id==%@",vehicle_id]; // If required to fetch specific vehicle
fetchRequest.predicate=predicate;
Vehicle *newVehicle=[[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] lastObject];

[newVehicle setValue: _txtFieldVehicleNumber.text forKey:@"number"];

[newVehicle setValue: lblFuelType.text forKey:@"fueltype"];

[newVehicle setValue: lblFuelUnit.text forKey:@"fuelunit"];

[newVehicle setValue: lblDistanceUnit.text forKey:@"distanceunit"];

[self.managedObjectContext save:nil]
4
  • 3
    can you please explain what this line means: Vehicle *newVehicle=[[self.managedObjectContext executeFetchRequest:fetchRequest error:nil] lastObject]; Sep 28, 2013 at 6:20
  • The above line fetches all the objects which exists in your core data store and which satisfies the criteria indicated by fetchRequest (if predicate is nil, then it fetches all records belonging to that entity). Since executeFetchRequest returns an array even if only one object satisfies the criteria, we need to do lastObject.
    – Max
    Sep 28, 2013 at 16:10
  • I have like a thousand records and obviously i have to use a for loop to update all the records. But this method takes like almost 5 minutes! Any suggestions?
    – Rashid
    Aug 22, 2015 at 8:27
  • The save statement should be outside the for loop. Does it take 5 minutes after that ?
    – Max
    Sep 14, 2015 at 1:29
3

Get an entity from core data:

VVdAppDelegate *delegate = (VVdAppDelegate *)[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *managedObjectContext = [delegate managedObjectContext];
NSEntityDescription *entityDescriptionDelegate = [NSEntityDescription entityForName:@"entityName" inManagedObjectContext:managedObjectContext];
NSFetchRequest *requestData = [[NSFetchRequest alloc] init];
[requestData setEntity:entityDescriptionDelegate];
NSError *error;

If there are more than an NSManagedObject

NSArray *objectsArray = [managedObjectContext executeFetchRequest:requestData error:&error];

for (NSManagedObject *object in objectsArray) {

        // Here update values of every object

}

// And save values to core data
[managedObjectContext save:&error];

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.