iPhone核心数据-简单查询

发布于 2024-08-18 23:28:20 字数 1428 浏览 11 评论 0原文

我正在尝试创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷情妓 2024-08-25 23:28:20

manufacturerNameList 将是 Car 实体的数组,而不是制造商名称。此外,您需要将 NSPropertyDescription 对象的 NSArray 传递给 setPropertiesToFetch,而不仅仅是属性名称。

以下是设置属性的方法:

NSDictionary *entityProperties = [ed propertiesByName];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:[entityProperties objectForKey:@"manufacturer"], nil]];

executeFetchRequest: 的结果将是 Car 实体的 NSArray,因此您必须提取循环中的 manufacturer 属性值或其他内容。

您可能需要考虑创建您的 Car 实体引用的 Manufacturer 实体,这将允许您以您现在尝试的方式查询更多信息。

manufacturerNameList is going to be an array of Car entities, not manufacturer names. Also, you need to pass an NSArray of NSPropertyDescription objects to setPropertiesToFetch not just attribute names.

Here is how you set the property:

NSDictionary *entityProperties = [ed propertiesByName];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:[entityProperties objectForKey:@"manufacturer"], nil]];

The results from executeFetchRequest: will be an NSArray of Car entities, so you'll then have to extract the manufacturer attribute values in a loop or something.

You may want to consider creating a Manufacturer entity that your Car entity references, that will allow you to query more in the way you are attempting to right now.

陪我终i 2024-08-25 23:28:20

另一种方法是为制造商创建一个实体,并在汽车和制造商之间建立关系,使得汽车有一个制造商,而制造商有许多汽车:

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.

赠我空喜 2024-08-25 23:28:20

最简单的解释是您有一个汽车实体,其制造商值为空。当谓词对获取进行排序时,空白字符串将排在第一位。

我会记录整个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文