Q:

PFQuery receiving different data order

I am using the following code to retrive some data, but the problem is every time I run the application I am receiving my data in a different order:
[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;
        }];
    }
}];

Is this a bug or is something wrong in my code ?

A:

According to the PFQuery docs, most location constraints, specifically whereKey:nearGeoPoint:, whereKey:nearGeoPoint:withinMiles:, whereKey:nearGeoPoint:withinKilometers:, and whereKey:nearGeoPoint:withinRadians:, force the query to return results "sorted by distance (nearest to farthest) from the reference point"; and so if your location changes at all, the result order can change too.
But the location constraint whereKey:withinGeoBoxFromSouthwest:toNortheast: does not force any distance sorting algorithm upon the results. So, as the answer Paulw11 linked to in his comment points out, you could use that method along with the [query orderByDescending:@"createdAt"]; to get the result order you desire.
The potential problem for you though, is that the whereKey:withinGeoBoxFromSouthwest:toNortheast: method checks within a square, the whereKey:nearGeoPoint:withinKilometers: checks within a circle radius, and they're generally different methods.
So if finding points within that circular kilometer radius and having the results sorted in the order in which they were created is important to you, perhaps you should sort your results after they're found with the query, ex:
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];

