[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 ??
To update a specific object you need to set up a NSFetchRequest . This class is equivalent to a SELECT statetement in SQL language. The array results contains all the managed objects contained within the sqlite file. If you want to grab a specific object (or more objects) you need to use a predicate with that request.
Add a Core Data Model to an Existing ProjectChoose File > New > File and select the iOS platform tab. Scroll down to the Core Data section, select Data Model, and click Next. Name your model file, select its group and targets, and click Create.
In some respects, an NSManagedObject acts like a dictionary—it's a generic container object that provides efficient storage for the properties defined by its associated NSEntityDescription instance.
One approach to delete everything and reset Core Data is to destroy the persistent store. Deleting and re-creating the persistent store will delete all objects in Core Data.
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With