NSFetchRequest"returnsDistinctResults"似乎被忽略了

编程入门 行业动态 更新时间:2024-10-22 16:27:04
本文介绍了NSFetchRequest"returnsDistinctResults"似乎被忽略了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试获取所有对象上属性的唯一值列表(类似于 [array valueForKeyPath:@"@ distinctUnionOfObjects.key"] .据我所知,我应该能够通过创建 NSFetchRequest 并将 resultType 设置为 NSDictionaryResultType 并指定 propertiesToFetch 和将 returnsDistinctResults 设置为 YES .这样做似乎不起作用.

I am attempting to get the a list of unique values for a property on all of my objects (similar to [array valueForKeyPath:@"@distinctUnionOfObjects.key"]. From what I can tell, I should be able to accomplish this by creating an NSFetchRequest with resultType set to NSDictionaryResultType, specifying the propertiesToFetch and setting returnsDistinctResults to YES. Doing this does not seem to work.

我创建了一个名为 Person 的实体,它具有三个属性 firstName , lastName 和 age .这是我尝试使用的代码:

I have created an entity called Person with the three attributes firstName, lastName and age. Here is the code I am trying to test this with:

NSManagedObjectModel *objectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:objectModel]; [coordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:nil]; NSManagedObjectContext *context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; context.persistentStoreCoordinator = coordinator; [context performBlockAndWait:^{ NSArray *lastNames = @[@"Smith",@"Johnson"]; NSArray *firstNames = @[@"John", @"Susan"]; for (NSString *lastName in lastNames) { for (NSString *firstName in firstNames) { Person *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:context]; person.firstName = firstName; person.lastName = lastName; person.age = @(18); } } }]; [context save:nil]; NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Person"]; fetchRequest.resultType = NSDictionaryResultType; fetchRequest.propertiesToFetch = @[@"age"]; fetchRequest.returnsDistinctResults = YES; NSArray *results = [context executeFetchRequest:fetchRequest error:nil]; NSLog(@"%@",results.description);

运行此命令时,这是我得到的日志:

When I run this, this is the log I get:

2017-10-12 16:17:54.723 TestCoreData[10650:879197] ( { age = 18; }, { age = 18; }, { age = 18; }, { age = 18; } )

将 propertiesToFetch 更改为 @ [@"lastName"] 会得到类似的结果:

Changing propertiesToFetch to @[@"lastName"] gives similar results:

2017-10-12 16:32:45.143 TestCoreData[10800:929908] ( { lastName = Smith; }, { lastName = Johnson; }, { lastName = Johnson; }, { lastName = Smith; } )

我的期望是,这些只会为 age 返回一个条目,为 lastName 返回两个条目.

My expectation would be that these would only return a single entry for age and two for lastName.

我误会了用法 returnsDistinctResults 或使用不正确?

Am I misunderstanding the use of returnsDistinctResults or using it incorrectly?

推荐答案

所以事实证明是因为我使用的是内存中存储.如果我改用SQLite存储支持此操作,则它可以按预期工作,并给出以下结果:

So it turns out this is occurring because I am using an in memory store. If I instead back this with an SQLite store, it works as expected, giving these results:

2017-10-12 17:21:48.546 TestCoreData[11767:1035544] ( { age = 18; } )

2017-10-12 17:23:52.632 TestCoreData[11803:1044179] ( { lastName = Johnson; }, { lastName = Smith; } )

我的猜测是,独特的功能是查询语句中发生的,并不存在于针对内存存储区的提取中.

My guess is that the distinct functionality is something that occurs in the query statement and is not not present for fetches against in memory stores.

更多推荐

NSFetchRequest"returnsDistinctResults"似乎被忽略了

本文发布于:2023-11-24 09:00:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1624569.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被忽略了   NSFetchRequest   quot   returnsDistinctResults

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!