PFQuery接收不同的数据顺序

时间:2014-12-16 22:50:15

标签: ios parse-platform

我使用以下代码来检索一些数据,但问题是我每次运行应用程序时都会以不同的顺序接收数据:

[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {

    if (!error) {

        NSDate *currentDate = [NSDate date];

        __block PFQuery *query = [[PFQuery alloc] initWithClassName:@"orders"];

        [query setLimit:25];

        [query whereKey:@"location" nearGeoPoint:geoPoint withinKilometers:kilometers];
        [query whereKey:@"createdAt" lessThanOrEqualTo:currentDate];

        [query orderByDescending:@"createdAt"];

        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (objects.count > 0) {

                [self.flebData removeAllObjects];
                [self.flebData addObjectsFromArray:objects];

                dispatch_async(dispatch_get_main_queue(), ^{

                    [self.tableView reloadData];

                    [self.tableView.pullToRefreshView stopAnimating];
                    // once refresh, allow the infinite scroll again
                    self.tableView.showsInfiniteScrolling = YES;

                });
            }
            query = nil;
        }];
    }
}];

这是一个错误还是我的代码出了问题?

1 个答案:

答案 0 :(得分:3)

根据PFQuery docs,大多数地理位置限制,特别是whereKey:nearGeoPoint:whereKey:nearGeoPoint:withinMiles:whereKey:nearGeoPoint:withinKilometers:whereKey:nearGeoPoint:withinRadians:,强制查询返回结果&# 34;按距离(距离参考点最远)排序&#34 ;;因此,如果您的位置发生了变化,结果顺序也会发生变化。

但位置约束whereKey:withinGeoBoxFromSouthwest:toNortheast:不会强制对结果进行任何距离排序算法。因此,正如Paulw11在他的评论中所指出的那样,您可以使用该方法和[query orderByDescending:@"createdAt"];来获得您想要的结果顺序。

您可能遇到的问题是,whereKey:withinGeoBoxFromSouthwest:toNortheast:方法在一个正方形内检查,whereKey:nearGeoPoint:withinKilometers:检查在一个圆半径内,并且它们通常是不同的方法。

因此,如果在圆形公里半径中找到按照创建顺序排序结果的点对您来说很重要,也许您应该对results 进行排序在之后,他们会在查询中找到它们,例如:

PFQuery *query = [[PFQuery alloc] initWithClassName:@"orders"];

[query setLimit:25];

[query whereKey:@"location" nearGeoPoint:geoPoint withinKilometers:kilometers];
[query whereKey:@"createdAt" lessThanOrEqualTo:currentDate];

[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {

    if (objects.count > 0) {

        createdAtDescriptor = [[NSSortDescriptor alloc] initWithKey:@"createdAt" ascending:NO];
        sortDescriptor = [NSArray arrayWithObject:createdAtDescriptor];
        self.flebData = [objects sortedArrayUsingDescriptors:sortDescriptor];