NSFetchedResultsController获取特定值

时间:2014-02-25 17:31:57

标签: ios objective-c core-data nsfetchedresultscontroller

我正在为一家商店构建一个应用程序,您可以在其中注册销售并在白天订购。我有一个splitViewController,我正在使用项目附带的模板代码。我有我的NSFetchedResultsController,它获取我所有的托管对象。这些对象包含销售的所有细节(日,价格,时间,总价格)。 我想创建一个包含所有不同日期的数组。这是一个例子。这些是我用NSFetchedResultsController检索的销售:

周一25日

周一25日

星期二26日

周三27日

我想得到一个这样的数组:

周一25日

星期二26日

周三27日

我希望这是我的主TableView的数据源。当用户点击一行时,它会将当天的所有管理对象传递给详细的TableView。

问题是我找不到在fetchedResultsController中检索它的方法并将其设置为数据源。如果我将我的数组设置为数据源并添加一天,我会得到:CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. attempt to insert row 4 into section 0, but there are only 2 rows in section 0 after the update with userInfo (null)

NSFetchedResultsController的委托存在问题。这是我的代码:

- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
    return _fetchedResultsController;
}

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Sale" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];

// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];

// Edit the sort key as appropriate.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
NSArray *sortDescriptors = @[sortDescriptor];

[fetchRequest setSortDescriptors:sortDescriptors];

// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Master"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error]) {
     // Replace this implementation with code to handle the error appropriately.
     // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

_arrayOfDays = [NSMutableArray new];

[[_fetchedResultsController fetchedObjects] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if (![_arrayOfDays containsObject:[obj valueForKeyPath:@"date"]]) {
        [_arrayOfDays addObject:[obj valueForKeyPath:@"date"]];
    }
}];

NSLog(@"%@", _arrayOfDays);

return _fetchedResultsController;
}

这里是我的numberOfRowsInSection:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
NSLog(@"Number of self.arrayOfDays: %d", (int)_arrayOfDays.count);
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
//return [sectionInfo numberOfObjects];
return self.arrayOfDays.count;
}

1 个答案:

答案 0 :(得分:1)

问题是您的提取请求与您实际想要显示的内容不匹配。您不能告诉您的FRC获取这些对象但是只显示具有匹配日期的任何项目的单行,它根本没有这种灵活性。您已自己手动完成过滤,因此每当FRC更改任何对象时,您需要在controllerDidChangeContent:中重做该手动过滤。来自FRC的插入没有意义,因为FRC不知道您正在使用更改版本的数组来支持表视图。请发布您的代码willChangeContent:didChangeObject:didChangeContent

老实说,如果你有一个与Sales有多对多关系的SaleDate实体,这听起来会更简单直接。在此视图中,您只需获取SaleDates,并使用与所选SaleDate关联的任何Sales加载详细信息表视图。这可以防止您必须手动过滤和管理自己的阵列,FRC已经涵盖了它。对于你想要完成的事情,这将是一个更好的方法。