0

I made an app which uses Core Data and fetched results controller.

I can add CoreData objects and also delete them. Now I want to update an CoreData object via fetched results controller. I know that I have to fetch the objects and then I can change it. But because I'm still learning I don't know how to do this. Now I'd like to ask you how to do this?

Apul Gupta
  • 3,044
  • 3
  • 22
  • 30
user3592462
  • 343
  • 1
  • 4
  • 9

1 Answers1

0

When you fetch from CoreData, if you modify the results it will update the actual value within CoreData when you save it.

You'll want to first perform your fetch:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Entity" inManagedObejctContext:moc]];
NSError *error = nil;
NSArray *results = [moc executeFetchRequest:request error:&error];

// error handling code

Once you have your results, you can modify the individual records...

MyEntity *entity = [results objectAtIndex:0];
entity.title = @"updated attribute";
// save context
[moc save:&error];

EDIT: In swift, it will be something along the lines of the following:

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
var moc = appDelegate.managedObjectContext!

var fetchRequest = NSFetchRequest()
fetchRequest.entity = NSEntityDescription.entityForName("Entity", inManagedObjectContext: moc)

var error: NSError?
var results = moc.executeFetchRequest(fetchRequest, error: &error)

// error handling code

var entity: MyEntity = MyEntity()
entity.title = "updated attribute"

moc.save(&error)
c_rath
  • 3,618
  • 2
  • 22
  • 14
  • Thanks for your answer. How can I do this in Swift? – user3592462 Apr 01 '15 at 14:12
  • I updated the answer to include swift code. I haven't worked too much with Swift, but it should be fairly similar to what I posted. – c_rath Apr 01 '15 at 14:21
  • you can add `let appDelegate = UIApplication.sharedApplication() as AppDelegate; var moc = appDelegate.managedObjectContext!;` Sorry, I skipped that part... I should have included it – c_rath Apr 01 '15 at 15:07
  • I get this error: `'UIApplication' is not convertible to 'AppDelegate'` and this error: `Use of undeclared type 'MyEntity'` – user3592462 Apr 01 '15 at 16:57
  • I'm guessing on the error on this one, but make sure you're using `UIApplication.sharedApplication().delegate`. Also, MyEntity is just an example of a possible entity you could use from CoreData. You'll need to replace that part with the actual CoreData entities and attributes that you have defined in your CoreData model. This is a good tutorial: http://jamesonquave.com/blog/core-data-in-swift-tutorial-part-1/ – c_rath Apr 01 '15 at 17:05
  • Yes, exactly. =) Put whatever you want there — so if you're updating a number you would put the new number value, etc. – c_rath Apr 01 '15 at 17:40
  • If I run and update a value I get the following error: `*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person setDatum:]: unrecognized selector sent to instance 0x7fcd00444bf0'` (Person is the entity). – user3592462 Apr 01 '15 at 17:44
  • Is Datum one of the attributes in your Person entity? If not, will you post your Person entity so I can see the attributes you have for it? – c_rath Apr 01 '15 at 17:47
  • Yes, datum is an attribute of the entity Person. – user3592462 Apr 01 '15 at 18:05
  • Without more details on your code and Data Model it's difficult to debug the issue. I did a quick search and found a post that has many troubleshooting options for that error regarding CoreData. http://stackoverflow.com/questions/4546451/coredata-issue-nsmanagedobject-setvalue-unrecognized-selector-sent-to-inst – c_rath Apr 01 '15 at 18:13
  • Ok thanks. But you know that I'm using fetched results controller? – user3592462 Apr 01 '15 at 18:14
  • Yes, modifying a CoreData object should behave the same though. There are plenty of tutorials explaining in detail how to work with the NSFetchedResultsController specifically, but the controller just manages how it's displayed and moved around. The fetching and updating doesn't really change. If you're still struggling on how to fetch and update, look over a few more tutorials and see if they include a solve for the issue. – c_rath Apr 01 '15 at 18:34