核心数据setReturnsDistinctResult不工作 [英] Core data setReturnsDistinctResult not working

查看:119
本文介绍了核心数据setReturnsDistinctResult不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我建立一个小应用程序,它使用核心数据数据库〜25mb大小与4个实体。这是公共汽车时刻表。

So i'm building a small application, it uses core data database of ~25mb size with 4 entities. It's for bus timetables.

在一个名为Stop的表中有〜1300个公交车站条目,其中有name,id,longitude,latitude 。现在有许多站点具有相同的名称属性,但不同的坐标和id。所以我想在表视图中显示所有不同的停止名称,我使用setReturnsDistinctResults与NSDictionaryResultType和setPropertiesToFetch。但setReturnsDistinctResult不工作,我还是得到所有的条目。

In one table named "Stop" there are ~1300 entries of bus stops with atributes "name", "id", "longitude", "latitude" and couple relationships. Now there are many stops with identical name attribute but different coordinates and id. So I want to show all distinct stop names in table view, i'm using setReturnsDistinctResults with NSDictionaryResultType and setPropertiesToFetch. But setReturnsDistinctResult is not working and I'm still getting all entries.

其他代码:

- (NSFetchRequest *)fetchRequest {
    if (fetchRequest == nil) {
        fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity =  [NSEntityDescription entityForName:@"Stop" inManagedObjectContext:managedObjectContext];
        [fetchRequest setEntity:entity];
        NSArray *sortDescriptors = [NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES] autorelease]];
        [fetchRequest setSortDescriptors:sortDescriptors];
        [fetchRequest setResultType:NSDictionaryResultType];
        [fetchRequest setPropertiesToFetch:[NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]]];
        [fetchRequest setReturnsDistinctResults:YES];
        DebugLog(@"fetchRequest initialized");
    }
    return fetchRequest;
}

/////////////////////

- (NSFetchedResultsController *)fetchedResultsController {
    if (self.predicateString != nil) {
        self.predicate = [NSPredicate predicateWithFormat:@"name CONTAINS[cd] %@", self.predicateString];
        [self.fetchRequest setPredicate:predicate];
    } else {
        self.predicate = nil;
        [self.fetchRequest setPredicate:predicate];
    }
    fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:sectionNameKeyPath cacheName:nil];

    return fetchedResultsController;
}  

//////////////

- (UITableViewCell *)tableView:(UITableView *)table cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [[fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"name"];

    return cell;
}


推荐答案

,但可能会帮助别人。

This may be late for you, but may help others.

我有同样的问题。我发现,如果持久化存储类型是 NSSQLiteStoreType ,returnDistinctResults工作。

I had the same problem. What I found is that, if persistant store type is NSSQLiteStoreType the returnDistinctResults works. But for NSXMLStoreType distinct values are not working.

我没有测试NSBinaryStoreType和NSInMemoryStoreType的结果。

I haven't tested results for NSBinaryStoreType and NSInMemoryStoreType.

这篇关于核心数据setReturnsDistinctResult不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆