Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to update core data entered values

[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 ??

like image 320
Paras Gorasiya Avatar asked Sep 27 '13 14:09

Paras Gorasiya


People also ask

How do you update an object in Core Data?

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.

How do I add Core Data to an existing project?

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.

What is NSManagedObject in Core Data?

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.

How do I clear my Core Data?

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.


1 Answers

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]
like image 88
Max Avatar answered Sep 19 '22 18:09

Max