0

I am building application that has 'checklist' functionality. The checklist item is stored as Core Data entity. There is a 'checked' attribute stored as BOOL in the Datamodel as well. The view controller handling this checklist functionality is based on UITableViewController.

Basically, I would like to implement the UIRefreshControl which allow users to reset the 'checked' status of all checklist entities in Core Data. For example, all the items would be reseted and shown as 'unchecked' once user pulls down the UITableView.

However, NSFetchedResultsController only provides access to one entity at a time via [fetchedResultsController objectAtIndexPath:indexPath]. Would there be a way to get the whole collection of entities from Core Data as NSArray or NSDictionary therefore I could enumerate all entities and change their 'checked' attribute?

3 Answers 3

1

Agrees with coverback...lets say you want to fetch all the objects from the entity named "Test":

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test"
                                          inManagedObjectContext:context];
NSError *error;
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];

fetchObjects array contains all the objects in "Test" entity

2
  • Thanks. I was focusing on using NSFetchedResultsController, and forgot that it's possible to fetch all the object directly via the managedObjectContext itself.
    – Kann
    Feb 18, 2013 at 14:56
  • And then you could call makeObjectsPerformSelector:withObject: on the array. But this could take a while if you have a lot of objects in the array. Jun 3, 2016 at 7:53
0

[fetchedResultsController fetchedObjects] will do just fine for your task.

In this case it is also not needed to use NSFetchedResultsController, you can get away with a simple NSFetchRequest.

0

I needed to update a lot of entities so the solution using NSFetchRequest were too slow. Luckily Apple added NSBatchUpdateRequest in iOS 8. Here's a simple example:

NSBatchUpdateRequest *batchUpdate = [[NSBatchUpdateRequest alloc] initWithEntityName:@"EntityName"];
batchUpdate.propertiesToUpdate = @{ @"attribute": @(0) };
batchUpdate.resultType = NSBatchDeleteResultTypeStatusOnly;
[managedObjectContext executeRequest:batchUpdate error:nil];

And here's a good blog post on the subject: https://www.bignerdranch.com/blog/new-in-core-data-and-ios-8-batch-updating/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.