5

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:-)

jwknz
  • 6,598
  • 16
  • 72
  • 115

2 Answers2

11

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.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
melsam
  • 4,947
  • 1
  • 22
  • 23
  • 2
    Note: `myObject.firstName = @"John";` only works if you created a NSManagedObject subclass. If you didn't do that, then you would need to do `[myObject setValue:@"John" forKey:@"firstName"]`. – fumoboy007 Jul 01 '12 at 07:32
  • 1
    I had this set up before I asked the question, but instead of updating a record it added it as a new one. – jwknz Jul 01 '12 at 23:50
8

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

Community
  • 1
  • 1
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • Hi, I still have one question sorry hope you don't mind:-) Instead of @"title == %@", @"Some Title"]; like in the link you gave me I use @"name == %@", self.appNameTxtFld.text]; Now the problem is that all my info rows in the array get changed not just the one I select. How can I be more specific in my actions? – jwknz Jul 02 '12 at 00:32
  • @JeffKranenburg Sorry, I cannot understand, maybe add an edit to your answer and explain it there. I will try to help you. Thanks. – Lorenzo B Jul 02 '12 at 07:27
  • Thanks for the offer to help, I went back to the drawing board as I ended up with so much code it started to look like spaghetti jungle:-) I got it working now, not exactly in this way but I understand it now. I'll keep you in my list of go to people when I have a Core Data issue. You seem to know that part pretty well:-) – jwknz Jul 03 '12 at 00:54