问题描述
我需要使用Parse进行简单的查询排序。
I need to do a simple query sort with Parse.
我有两个类: Captain
和船
。每个船长
都有一个 Boat
指针对象。
I have two classes: Captain
and Boat
. Every Captain
has a Boat
pointer object.
I列出表中包含其属性的所有 Captain
对象,例如名称, Boat
name等。
I list all of the Captain
objects in a table with their attributes, such as name, Boat
name etc.
我需要能够通过船$ $对
船长
对象的查询进行排序c $ c>名称。我该怎么办?
I need to be able to sort the query of Captain
objects, via the Boat
name. How can I?
我可以使用 NSSortDescriptor
排序键(例如<$ c $)对直接属性进行排序c>船长 名称
。但我需要按船$ c>的
名称
对船长
对象进行排序$ c>。
I can sort a direct attribute with an NSSortDescriptor
sort key such as the Captain
name
. But I need to sort the Captain
objects by the name
of their Boat
.
推荐答案
如果您可以在排序限定符上使用点表示法,那会很好,例如:
It would be nice if you could use dot notation on the sort qualifiers, like:
[captainQuery orderByAscending:@"boat.name"];
不幸的是,属性点符号仅适用于includeKey:。所以这样做的方法是获取,包括相关对象,然后使用相关对象的属性进行本地排序:
Unfortunately the attribute dot notation applies only to includeKey:. So the way to do this to fetch, including the related object, then sort locally using the attribute of the related objects:
// ...
[captainQuery includeKey:@"boat"];
[query findObjectsInBackgroundWithBlock:^(NSArray *captains, NSError *error) {
NSArray *sortedCaptains = [captains sortedArrayUsingComparator: ^(PFObject *capA, PFObject *capB) {
return [capA[@"boat"][@"name"] compare:capB[@"boat"][@"name"]];
// or reverse param order for descending
}];
}];
编辑 - @Logan帮助指出此答案无法扩展好吧,如果您的查询页面通过成千上万的船长。如果船长数量巨大,最好包括一艘船 - >船长指针,并执行以下操作:
EDIT - @Logan helpfully points out that this answer doesn't scale well if your query pages through thousands of captains. If the captain count is huge, it might be better to include a boat->captain pointer and do the following:
更多环绕,你可以取船,订购ByAscending在他们的名字上,includeKey他们的船长(如果你的船有一个指向船长的后指针),然后在返回的(分类的)船上映射他们的船长。
More roundabout-ly, you could fetch boats, orderByAscending on their name, includeKey their captains (if your boats have a back-pointer to captain), and then map over the returned (sorted) boats for their captains.
这篇关于通过指针解析排序查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!