简体   繁体   English

尝试按日期分组核心数据获取结果

[英]Trying To Group Core Data Fetch Results By Date

I want to group core data entries by week and date. 我想按星期和日期对核心数据条目进行分组。 I have a core data entity named WorkEntry which among it's attributes I have weekBegin and date . 我有一个名为WorkEntry的核心数据实体,在它的属性中,我具有weekBegindate In these I save the date it was saved on as well as the first day of the week corresponding to that date. 在这些文件中,我保存了保存日期以及该日期对应的一周的第一天。 I use weekBegin for the section name and I want the cells to show the dates that have entries with no duplicates. 我将weekBegin用作节名,并且希望单元格显示具有重复项的日期。 I used http://felipecypriano.com/2011/09/21/core-data-how-to-do-a-select-distinct/ as a guide. 我使用http://felipecypriano.com/2011/09/21/core-data-how-to-do-a-select-distinct/作为指南。 Here's my code: 这是我的代码:

Fetch Request with returnsDistinctResults : 取得带有returnsDistinctResults请求:

- (NSFetchRequest *)workDayFetchRequest {
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"WorkEntry"];
    CoreDataStack *coreDataStack = [CoreDataStack defaultStack];

    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WorkEntry" inManagedObjectContext:coreDataStack.managedObjectContext];
    fetchRequest.entity = entity;
    fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"weekBegin"]];
    fetchRequest.returnsDistinctResults = YES;
    fetchRequest.resultType = NSDictionaryResultType;

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"weekBegin" ascending:YES];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    NSError *error = nil;
    self.distinctResults = [coreDataStack.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    return fetchRequest;
}

which gives me an array called distinctResults which I use in: 这给了我一个叫做distinctResults的数组,该数组用于:

CellForRowAtIndexPath : CellForRowAtIndexPath

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    cell.textLabel.text = [self.distinctResults objectAtIndex:indexPath.row];

    return cell;
}

But I have a feeling that may not be right. 但是我有一种不正确的感觉。 When I switch to the table view in the app it crashes with this exception: 当我在应用程序中切换到表格视图时,它因以下异常而崩溃:

-[NSKnownKeysDictionary1 length]: unrecognized selector sent to instance 0x8c7c8a0

I'd be really grateful for any help you guys can give. 非常感谢你们能提供的任何帮助。 I've been going crazy trying to figure this out. 我一直在疯狂试图解决这个问题。 Thanks 谢谢

The problem may be in the way you declare distinctResults property in you controller. 问题可能出在您在控制器中声明distinctResults属性的方式。 It may be deallocated if you use weak pointer. 如果使用弱指针,它可能会被释放。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM