核心数据对象通过循环更新

时间:2014-01-07 02:09:49

标签: ios core-data

我有tableView显示core-data objects。在同一视图中有五个按钮。每个按钮操作都应该更新对象的属性值。 作为一个例子,我将向您展示我必须更新属性'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];

}

此方法抛出以下异常:

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'

我需要什么来避免异常并获得所需的更新? 这是我的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 个答案:

答案 0 :(得分:3)

好的,我对核心数据一无所知,但是从我在最后两分钟研究过的内容看来,好像你没有遍历NSManagedObjectContext的对象。您需要在上下文中创建搜索并从中获取结果。然后迭代结果并修改willy nilly。

以下是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];

因此,获取上下文,创建搜索,根据搜索条件从上下文中获取数组,循环搜索结果并按照您的意愿进行更新。

答案 1 :(得分:1)

如果您使用fetchedResultsController已经填充tableView,那么您可以像这样迭代fetchedResultsController中的对象:

- (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);
    }

}

顺便说一句,你应该检查你的保存呼叫中的错误,所以不要传递nil。