xcode 避免 Core Data fetch 上的重复结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11233789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
avoid duplicate results on Core Data fetch
提问by Marcal
I have a subclass of the CoreDataTableViewController (subclass of UITAbleViewController dome by the people on Stanford done to link CoreData and TableViews). On this Class, I want to perform a fecth, sorting by an attribute called "definition" and the code which executes it is the following:
我有一个 CoreDataTableViewController 的子类(斯坦福大学的人为链接 CoreData 和 TableView 所做的 UITableViewController 圆顶的子类)。在这个类上,我想执行一个 fecth,按一个名为“定义”的属性排序,执行它的代码如下:
- (void)setupFetchedResultsController{
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:self.entity];
request.propertiesToFetch=[NSArray arrayWithObject:@"definition"];
request.returnsDistinctResults=YES;
NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"%K != nil", @"definition"];
NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"%K != ''", @"definition"];
NSPredicate *predicate3= [NSPredicate predicateWithFormat:@"%K contains[cd] %@", @"definition", self.seachBar.text];
NSArray *prepredicateArray;
if ([self.seachBar.text length]) {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2, predicate3,nil];
}else {
prepredicateArray = [NSArray arrayWithObjects:predicate1, predicate2,nil];
}
request.predicate=[NSCompoundPredicate andPredicateWithSubpredicates:prepredicateArray];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"definition" ascending:YES ]];
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self performFetch];
}
If I understood it correctly, setting request.returnsDistinctResults=YES; should avoid fetching duplicates. However it doesn't work and I'm seeing duplicates of this attribute's value.
如果我理解正确,设置 request.returnsDistinctResults=YES; 应该避免获取重复项。但是它不起作用,我看到此属性值的重复项。
Is there something I'm missing there? I'd appreciate some pointings there. Thank you in advance.
有什么我在那里想念的吗?我很感激那里的一些指点。先感谢您。
EDIT: If anyone is having the same issue here, after applying David's answer the resulting fetchedResultsController is just a NSDIctionary with object with only the requested value, which for displaying only purposes is quite fine. One thing I've done in cellForRowAtIndexPath in order to display the results on the cell label is:
编辑:如果有人在这里遇到同样的问题,在应用 David 的回答后,结果 fetchedResultsController 只是一个 NSDictionary 对象,只有请求的值,仅用于显示目的非常好。为了在单元格标签上显示结果,我在 cellForRowAtIndexPath 中所做的一件事是:
Before:
之前:
HNMR *hnmr = [self.fetchedResultsController objectAtIndexPath:indexPath];
cell.textLabel.text=hnmr.definition;
After:
之后:
cell.textLabel.text=[[self.fetchedResultsController objectAtIndexPath:indexPath] valueForKey:@"definition"];
回答by David R?nnqvist
From the documentation of returnsDistinctResults
:
This value is only used if a value has been set for
propertiesToFetch
.
仅当为 设置了值时才使用此值
propertiesToFetch
。
From the documentation of propertiesToFetch
:
This value is only used if
resultType
is set toNSDictionaryResultType
.
此值仅在
resultType
设置为 时使用NSDictionaryResultType
。
From the documentation of resultType
:
The default value is
NSManagedObjectResultType
.
默认值为
NSManagedObjectResultType
。
This all tells me that the propertiesToFetch
is ignored because you haven't set the resultType
yourself and the default it to return managed objects instead of dictionaries. Since the propertiesToFetch
is ignored the returnsDistinctResults
is ignored as well and thus you are still getting duplicates.
这一切都告诉我,propertiesToFetch
被忽略了,因为您没有设置resultType
自己,并且默认它返回托管对象而不是字典。由于propertiesToFetch
被忽略returnsDistinctResults
也被忽略,因此您仍然会收到重复项。
Try setting the result type to return dictionaries instead of managed objects.
尝试将结果类型设置为返回字典而不是托管对象。
request.resultType = NSDictionaryResultType;
回答by Lorenzo B
In addition to David R?nnqvistanswer I suggest a useful link (with a sample) on selecting distinct values with Core Data:
除了David R?nnqvist 的回答之外,我还建议使用一个有用的链接(带有示例)来使用 Core Data 选择不同的值:
core-data-how-to-do-a-select-distinct
core-data-how-to-do-a-select-distinct
Hope that helps.
希望有帮助。