1

Can a fetchedResultsController return an Array of Arrays? Or An array of NSSets?

Code below is an example, of what I'm trying to do. Is it possible?

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //...
    NSSet *objects = [_fetchedResultsController objectAtIndexPath:indexPath];
    //...
}

The reason I'm doing this, is: if the user swipes the cell, and deletes it. I need all objects from that row deleted. And I also need to display data on that Cell, from calculation made on all the clocks for that Day/Row.

Here's my Core Data Model:

enter image description here

Each row must contain all Clock objects, for a given day, based on it's clockIn property. clockIn is a NSDate object. One row should represent one day.

Could I get help figuring out a predicate for this? Is it even possible?

Also, I'm already using sections in my Table View. So these are out of the question. There is one solution that I rather no go for, which is to create Year, Month and Day, Entities. This would fix. But it seems odd that I need to do that, considering the my clockIn property should give me everything I need.

4

2 回答 2

0

不,它不能。

如此处所述:NSPredicate 相当于 SQL 的 GROUP BY

“CoreData 是一个对象图管理框架,而不是 SQL 数据存储。因此,您应该尽快摆脱 SQL 思维模式。”

关于手中的问题:

“NSPredicate 旨在作为对象图中限定对象的谓词(不足为奇)。因此,您可以获取与查询匹配的所有对象,但这与对这些结果进行分组不同。如果您想进行聚合操作, "

于 2013-07-25T18:14:16.533 回答
0

您可以使用TLIndexPathTools通过子类化几行代码来完成此操作,这TLIndexPathControllerNSFetchedResultsController. 我没有对此进行测试,但这里是它的样机:

#import <TLIndexPathController.h>
@interface GroupedIndexPathController : TLIndexPathController
@end

#import "GroupedIndexPathController.h"
#import "TLIndexPathSectionInfo.h"
@implementation GroupedIndexPathController

- (void)setDataModel:(TLIndexPathDataModel *)dataModel
{
    NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:dataModel.numberOfSections];
    for (TLIndexPathSectionInfo *sectionInfo in dataModel.sections) {
        if (sectionInfo.objects) {
            [items addObject:sectionInfo.objects];
        }
    }
    TLIndexPathDataModel *groupedDataModel = [[TLIndexPathDataModel alloc] initWithItems:items andSectionNameKeyPath:nil andIdentifierKeyPath:nil];
    [super setDataModel:groupedDataModel];
}

@end

您要做的是将索引路径控制器的获取请求sectionNameKeyPath设置为您要分组的属性。执行获取时,父类将生成一个组织成部分的数据模型。上面的代码覆盖了dataModel属性设置器,将这些部分映射到分组对象的单个部分,例如:

分段数据模型

Section 0
    Managed Object 0.0
    Managed Object 0.1
    Managed Object 0.2
Section 1
    Managed Object 1.0
    Managed Object 1.1

分组数据模型

Section 0
    [Managed Object 0.0, Managed Object 0.1, Managed Object 0.2]
    [Managed Object 1.0, Managed Object 1.1]

然后将该分组数据模型传递给 super 的dataModel属性设置器。表视图控制器只会看到分组数据模型。分段数据模型将完全在GroupedIndexPathController. 如果获取结果发生变化,将通过委托方法将GroupedIndexPathController更新报告给分组数据模型,并自动为您执行批量更新。controller:didUpdateDataModel:TLTableViewController

TLIndexPathTools 提供了几个示例项目,Core Data 示例演示了如何与 Core Data 集成。您只需要提供一个实例GroupedIndexPathController而不是标准TLIndexPathController.

您提到您的表格使用部分。如果您能详细说明,我可以修改以上内容以满足您的特定要求。

于 2013-07-26T01:18:18.973 回答