iPhone核心数据-简单查询
我正在尝试创建一个 Core Data iPhone 应用程序。我跟踪的实体之一是汽车,每辆车的一个属性是“制造商”。
在我的应用程序的“编辑汽车”部分中,我有一个 UIPickerView,需要加载之前输入系统的每个唯一制造商。我想做的是创建一个 NSFetchRequest 来获取一组唯一的“制造商”属性,并使用它来填充 UIPickerView。
我遇到的问题是,无论数据存储中有 0 条记录还是 100 条记录,在执行的提取请求中,元素 0 处始终有一条值为 @"" 的记录。
我做错了吗还是有更简单的方法来做到这一点?我希望我可以运行一个快速的 sql 查询!
我的代码如下:
// Populate the manufacturerNameList array
NSManagedObjectContext *moc = [self.selectedLease managedObjectContext];
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:ed];
// Get only manufacturer and only uniques
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"manufacturer",nil]];
[fetchRequest setReturnsDistinctResults:YES];
// Sort by manufacturer in ascending order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"manufacturer" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
self.manufacturerNameList = [moc executeFetchRequest:fetchRequest error:&error];
if (error) {
// Handle the error
}
NSLog(@"The count of self.propertyNameList is %i",[self.propertyNameList count]);
谢谢!
I am trying to create a Core Data iPhone application. One of the entities I'm tracking is cars, and one attribute of each car is "manufacturer".
In the "edit car" section of my application, I have a UIPickerView that needs to be loaded with each of the unique manufacturers that have previously been entered into the system. What I'm trying to do is create an NSFetchRequest to get an array of unique "manufacturer" attributes and use that to populate the UIPickerView.
The problem I'm running into is that whether there are zero records or 100 in the data store, there is always one record in the executed fetch request at element zero with a value @"".
Am I doing this wrong or is there an easier way to do this? I wish I could just run a quick sql query!!!
My code is below:
// Populate the manufacturerNameList array
NSManagedObjectContext *moc = [self.selectedLease managedObjectContext];
NSEntityDescription *ed = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
[fetchRequest setEntity:ed];
// Get only manufacturer and only uniques
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"manufacturer",nil]];
[fetchRequest setReturnsDistinctResults:YES];
// Sort by manufacturer in ascending order
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"manufacturer" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSError *error = nil;
self.manufacturerNameList = [moc executeFetchRequest:fetchRequest error:&error];
if (error) {
// Handle the error
}
NSLog(@"The count of self.propertyNameList is %i",[self.propertyNameList count]);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
manufacturerNameList
将是Car
实体的数组,而不是制造商名称。此外,您需要将NSPropertyDescription
对象的NSArray
传递给setPropertiesToFetch
,而不仅仅是属性名称。以下是设置属性的方法:
executeFetchRequest:
的结果将是Car
实体的NSArray
,因此您必须提取循环中的manufacturer
属性值或其他内容。您可能需要考虑创建您的
Car
实体引用的Manufacturer
实体,这将允许您以您现在尝试的方式查询更多信息。manufacturerNameList
is going to be an array ofCar
entities, not manufacturer names. Also, you need to pass anNSArray
ofNSPropertyDescription
objects tosetPropertiesToFetch
not just attribute names.Here is how you set the property:
The results from
executeFetchRequest:
will be anNSArray
ofCar
entities, so you'll then have to extract themanufacturer
attribute values in a loop or something.You may want to consider creating a
Manufacturer
entity that yourCar
entity references, that will allow you to query more in the way you are attempting to right now.另一种方法是为制造商创建一个实体,并在汽车和制造商之间建立关系,使得汽车有一个制造商,而制造商有许多汽车:
Car <<-->制造商
制造商实体可以有一个字符串属性“名称”。
然后,您可以通过获取所有制造商对象并查看“名称”属性来获取制造商名称的完整列表。
Another approach would be to create an entity for manufacturers and have a relationship between Car and Manufacturer such that a Car has one Manufacturer and a Manufacturer has many Cars:
Car <<--> Manufacturer
The Manufacturer entity could have a string attribute its "name".
Then, you could get the full list of manufacturer names by fetching all the Manufacturer objects and looking at the "name" property.
最简单的解释是您有一个汽车实体,其制造商值为空。当谓词对获取进行排序时,空白字符串将排在第一位。
我会记录整个 self.propertyNameList 并查看您实际返回的内容。
The simplest explanation is that you have a car entity that has an empty manufacturer value. When the predicate sorts the fetch, the blank string will be ranked first.
I would log the entire
self.propertyNameList
and see what you're actually getting back.