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

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

问题描述

所以在查看核心数据片段,找到如下代码:

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
}

这很棒,看起来很适合我需要的东西……但是人们如何实际使用它呢?我假设因为它正在返回字典,所以我需要一个键来获取值 - 但是键在哪里定义?那是id requestsValue = nil"行吗?如果是这样,requestedValue"如何成为关键?Xcode 在requestedValue"声明中给了我一个关于未使用变量的编译器警告.我觉得我在这里遗漏了一些东西.

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 什么都不是,显然没有被使用(也许它是其他一些示例的一部分,被剪切并粘贴到此示例代码中——谁知道呢).你可以忽略它.

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"]);
}
// ...

因此,如果您的数据存储中有 5 个名为 Bob、Sally、Joe、Freida 和 Sue 的人,您会看到这些名字被打印出来.如果您想使用任何其他属性(例如它们的年龄),您必须将其添加到您在 setPropertiesToFetch: 中设置的数组中.

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天全站免登陆