Sorting results from Parse is not working as expected with query.limit

ε祈祈猫儿з 提交于 2019-12-25 02:25:27

问题


I set a limit for my query to be 10 for example and then I want to get the last 10 results in the data set AND sorted with ascending time! Okay I know I can add:

[messages addDescendingOrder:@"createdAt"];
//OR
[messages addAscendingOrder:@"createdAt"];

But what this is doing is that it is getting the first 10 records or the last 10 records... It is not sorting the results themselves!! The ascending order is basically setting where the results are going to start... From bottom or from top. But I want the results set coming back to be last 10 results and those results in ascending order. Instead, I am getting last 10 results but in the table I have the newest one on the top and the oldest one in the set to be the last cell in the table... I want the reverse of that...

Is there a way to have Parse take care of this rather than making logic to do it on the device itself? Any help is greatly appreciated.


回答1:


Ok so for others who are having a similar problem, the solution is to reverse the results array that you get from the query back from Parse. Done :) Happy Coding!

I used the code that I found in a different question about reversing arrays on StackOverflow but here is the code again:

@implementation NSArray (Reverse)

- (NSArray *)reversedArray {
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:[self count]];
    NSEnumerator *enumerator = [self reverseObjectEnumerator];
    for (id element in enumerator) {
        [array addObject:element];
    }
    return array;
}

@end

@implementation NSMutableArray (Reverse)

- (void)reverse {
    if ([self count] == 0)
        return;
    NSUInteger i = 0;
    NSUInteger j = [self count] - 1;
    while (i < j) {
        [self exchangeObjectAtIndex:i
                  withObjectAtIndex:j];

        i++;
        j--;
    }
}

@end


来源:https://stackoverflow.com/questions/24622925/sorting-results-from-parse-is-not-working-as-expected-with-query-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!