0

I have a tableView showing core-data objects. On the same view there are five buttons. Each button action should update the value of an attribute from the objects. As an example, I will show you what I have to update the attribute 'isDone':

- (IBAction)allDoneAction:(id)sender {

    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    int i=0;
    for (NSManagedObject *mo in context)
    {
        [mo setValue:@"Done" forKey:@"isDone"];i++;
    }

    [managedObjectContext save:nil];

}

This method throws following following exception:

NSManagedObjectContext countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9a6b0a0
2014-01-06 19:01:43.862 To-Do Pro[679:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSManagedObjectContext countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x9a6b0a0'

What do I need to avoid the exception and obtain the desired update? Here is my NSFetchedResultsController:

- (NSFetchedResultsController *)fetchedResultsController
{
  if (fetchedResultsController) return fetchedResultsController;

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
  NSEntityDescription *entity = 
               [NSEntityDescription entityForName:@"FavoriteThing" 
                           inManagedObjectContext:managedObjectContext];

  [fetchRequest setEntity:entity];

  NSSortDescriptor *sortDescriptor = 
              [[NSSortDescriptor alloc] initWithKey:@"displayOrder" 
                                          ascending:YES];

  NSArray *sortDescriptors = [[NSArray alloc] 
                              initWithObjects:sortDescriptor, nil];
    //SOLO TO-DOS DE TODAY


    todayDate = [NSDate date];

    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit fromDate:todayDate]; // Get necessary date components




    NSNumber *yearBuscado = [NSNumber numberWithLong:[components year]];
    NSNumber *mesBuscado = [NSNumber numberWithLong:[components month]];
    NSNumber *diaBuscado = [NSNumber numberWithLong:[components day]];

   // NSString *tipourgente = @"Urgent";
   // NSString *tipocolor = @"Yellow";

    NSString *textoNotDone = @"Not done";
    NSString *textoNotDeleted = @"Not deleted";

    NSPredicate *yearPredicate = [NSPredicate predicateWithFormat:@"todoYear == %@", yearBuscado];
    NSPredicate *monthPredicate = [NSPredicate predicateWithFormat:@"todoMonth == %@", mesBuscado];
    NSPredicate *dayPredicate = [NSPredicate predicateWithFormat:@"todoDay == %@", diaBuscado];
    NSPredicate *notDonePredicate = [NSPredicate predicateWithFormat:@"isDone== %@", textoNotDone];
    NSPredicate *notDeletedPredicate = [NSPredicate predicateWithFormat:@"isSemiDeleted==%@", textoNotDeleted];
    // NSPredicate *urgentPredicate = [NSPredicate predicateWithFormat:@"urgent == %@", tipourgente];
   // NSPredicate *colorPredicate = [NSPredicate predicateWithFormat:@"color == %@", tipocolor];

  [fetchRequest setSortDescriptors:sortDescriptors];






  NSPredicate *busqueda = [NSCompoundPredicate andPredicateWithSubpredicates:@[yearPredicate,monthPredicate,dayPredicate,notDonePredicate,notDeletedPredicate]];

    [fetchRequest setPredicate:busqueda];
  NSFetchedResultsController *aFetchedResultsController =
              [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest 
                                                  managedObjectContext:managedObjectContext
                                                    sectionNameKeyPath:nil cacheName:nil];
  aFetchedResultsController.delegate = self;
  [self setFetchedResultsController:aFetchedResultsController];


  [aFetchedResultsController release];
  [fetchRequest release];
  [sortDescriptor release];
  [sortDescriptors release];


  return fetchedResultsController;
}    

2 Answers 2

3

Ok, I know absolutely nothing about core data, but from what I have researched in the last two minutes it appears as though you do not loop over An object of NSManagedObjectContext. You need to create a search within the context and get the results from that. Then iterate through the results and modify willy nilly.

Here is an example taken from this answer:

NSManagedObjectContext * context = [self managedObjectContext];
NSFetchRequest * fetch = [[[NSFetchRequest alloc] init] autorelease];
[fetch setEntity:[NSEntityDescription entityForName:@"ShoppingBasket" inManagedObjectContext:context]];
NSArray * result = [context executeFetchRequest:fetch error:nil];
for (id basket in result)
    [context deleteObject:basket];

So get the context, Create a search, get an array from the context based on the search criteria, loop through the results and do as you will with updating them.

Sign up to request clarification or add additional context in comments.

2 Comments

Presumably he has already done a fetch with the fetchedResultsController so no point in doing another fetch. See my answer to use the existing fetchedResultsController result set.
I agree that duplicate fetches would be rather stupid and time wasting. Thanks for the information, if I ever get into core data it will come in handy.
1

If you are using a fetchedResultsController already to populate the tableView then you can iterate over the objects in the fetchedResultsController like this:

- (IBAction)allDoneAction:(id)sender {

    NSArray *objects = [fetchedResultsController fetchedObjects];

    for (NSManagedObject *mo in objects) {
       [mo setValue:@"Done" forKey:@"isDone"];i++;
    }

    NSError *error;
    bool result = [[fetchedResultsController managedObjectContext] save:&error];

    if (!result) {
       NSLog(@" error saving context, %@, %@", error, error.userInfo);
    }

}

BTW you should be checking for errors in your call to save so don't pass in nil.

3 Comments

Yes, I am populating the tableView using a fetchedResultsController. Your proposal, adding int i=0; shows a warning at 'fetchResults' and an exception at execution: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSFetchedResultsController fetchedResults]: unrecognized selector sent to instance
post the code you are using to create the fetchedResultsController
No, don't bother I made a mistake its fetchedObjects not fetchedResults, see the corrected code.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.