如何在Core Data中获取不同的值? [英] How to fetch distinct values in Core Data?

查看:138
本文介绍了如何在Core Data中获取不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在查看核心数据片段,我找到了以下代码:

  ... 
[request setEntity:entity];
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctValues:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@< #Attribute name#>]];

//执行fetch
NSError *错误;
id requestedValue = nil; // WTF?这不是定义或使用任何地方
NSArray * objects = [managedObjectContext executeFetchRequest:request error:& error];
if(objects == nil){
//处理错误
}

这是伟大的,似乎完美的我需要...但实际上如何使用它?我认为,因为它返回的字典,我需要一个键来获得的价值观 - 但在哪里的关键定义?那是id requestedValue = nil行吗?如果是这样,requestedValue如何成为key? Xcode给我一个关于在requestedValue声明未使用的变量的编译器警告。

>解决方案

requestedValue是没有什么,显然没有使用(也许是一些其他例子的一部分,剪切和粘贴到这个示例代码 - 谁知道)。您可以忽略它。



字典的键与您提取并在模型中定义的属性相同。因此,例如,如果您有一个person实体具有定义的三个属性name,age,phoneNumber,并且您只请求名称,那么这将是您的字典中数据的唯一键。所以:

  [request setEntity:entity]; 
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@name]];

//执行fetch
NSError *错误;
NSArray * objects = [managedObjectContext executeFetchRequest:request error:& error];
if(objects == nil){
abort();
}
for(NSDictionary * obj in objects){
NSLog(@Person:%@,[obj objectForKey:@name]);
}
// ...

在名为Bob,Sally,Joe,Freida和Sue的数据存储中,您将看到这些名称打印出来。如果你想使用任何其他属性(如他们的年龄),你必须添加到你在 setPropertiesToFetch:中设置的数组。



在大多数情况下,最好只是简单地检索受管对象。对象将被故障,所以你甚至不会将属性带入内存,除非你实际访问它们。此外,如果您要对对象进行更改,您可以将其持久保存到商店(使用托管对象)。


So in looking through Core Data Snippets, I found the following code:

...
[request setEntity:entity]; 
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctValues:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"<#Attribute name#>"]];

// Execute the fetch
NSError *error;
id requestedValue = nil; // WTF? This isn't defined or used anywhere
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
     // handle the error
}

This is great and seems perfect for what I need...but how does one actually use it? I assume since it's returning dictionaries, I need a key to get at the values - but where's the key defined? Is that the "id requestedValue = nil" line? If so, how does "requestedValue" become the key? Xcode gives me a compiler warning about an unused variable at the "requestedValue" declaration. I feel like I'm missing something here.

Thanks in advance for any assistance you can offer.

解决方案

requestedValue is nothing and is obviously not being used (perhaps it was part of some other example that got cut&paste into this example code--who knows). You can just ignore it.

The keys for the dictionary are the same as the attributes you fetched and are defined in your model. So, for example, if you had a person entity with three attributes defined, name, age, phoneNumber, and you requested only name, that would be the only keys with data in your dictionaries. So:

[request setEntity:entity]; 
[request setResultType:NSDictionaryResultType];
[request setReturnsDistinctResults:YES];
[request setPropertiesToFetch:[NSArray arrayWithObject:@"name"]];

// Execute the fetch
NSError *error;
NSArray *objects = [managedObjectContext executeFetchRequest:request error:&error];
if (objects == nil) {
     abort();
}
for( NSDictionary* obj in objects ) {
  NSLog(@"Person: %@", [obj objectForKey:@"name"]);
}
// ...

So, if you have 5 people in your data store named Bob, Sally, Joe, Freida and Sue, you would see those names print out. If you want to use any of the other attributes (like their age), you would have to add that to the array you set in setPropertiesToFetch:.

In most cases, it is probably best to simply retrieve the managed object, however. The object will be faulted so you won't even bring attributes into memory unless you actually access them. Plus, if you want to make changes to the objects, you could and persist them to the store (with managed objects).

这篇关于如何在Core Data中获取不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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