Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios - Core Data updating records

I am currently looking through documentation and the web, but I am not seeing what I am looking for and I would just like to confirm if what I am looking for exists:-)

As I understand it - this line of code allows me to add a record to the SQLite database which I am showing in my app through Core Data:

NSManagedObject *newSoftware = [NSEntityDescription insertNewObjectForEntityForName:

Now is there a line similar to that that allows me to update the record I am working on?

something like this?

NSManagedObject *newSoftware = [NSEntityDescription updateCurrentObjectForEntityForName:

Thank you very much for the feedback:-)

like image 507
jwknz Avatar asked Jul 01 '12 05:07

jwknz


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 does Core Data work in iOS?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.

When should I use Core Data?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

Should I use Core Data or realm?

If your project requires encryption or speed, then Realm is an obvious choice. If your project has a complex data model that changes frequently, then Core Data might be a better choice.


2 Answers

To update a record, you simply update the NSManagedObject's properties. If you're using CoreData, you should have NSManagedObjects for each of your "tables". Get a reference to an object after you've queried the database (using NSFetchRequest). Then you can update the record like this: myObject.firstName = "John"; When you're ready to update the object in your Sqlite database, just call your NSManagedContext's save method. I suggest play with a CoreData sample app (like the default one generated by Xcode) to understand this better.

like image 144
melsam Avatar answered Sep 19 '22 08:09

melsam


Here my previous answer on it. It is quite detailed to understand what is going on.

How to update existing object in Core Data?

Based on fumoboy007 comment, you have to update your object trough KVC if you have not set up a subclass for your managed object. If you haven't already I suggest to create it. The code looks like more cleaner since you can acess a managed object through properties.

Here a link on it:

organising-core-data-for-ios

like image 21
Lorenzo B Avatar answered Sep 19 '22 08:09

Lorenzo B