Setting PFQuery Order

I want mine to PFQuery

come in random order, so the next time I create the same one PFQuery

with a constraint, it will not return the same objects as the first one.

PFQuery *query = [PFUser query];
[query orderBy...]; //Is there a randomOrder method?
                    //Or a workaround to get random order?
[query setLimit:10];

      

I need this to be randomized every time, otherwise it PFQuery

will contain the same 10 objects every time

+3


source to share


3 answers


You cannot change the order of the data returned in the request, but you can use paging to change the first returned object - so you can do something like this (it is based on the ToDo code example from Parse, but it will work for any object) -

PFQuery *query =[PFQuery queryWithClassName:@"Todo"];

NSInteger count=[query countObjects];
NSInteger skip = arc4random_uniform(count-10);

query.skip=skip;
query.limit=10;

NSArray *results=[query findObjects];

NSLog(@"object count=%d",results.count);

for (PFObject *object in results) {
    NSLog(@"text=%@",object[@"text"]);
}

      

You can now get 10 objects. for any given number of passes they will be in the same order, but you can randomize the order after you have received 10 items. Just put them in NSMutableArray

and use the technique in this answer - Rebuild NSArray / MSMutableArray in random order



Note that this code is not optimal because it does not perform fetch tasks on a background thread. To use background threads, you have to use something like the following -

PFQuery *query =[PFQuery queryWithClassName:@"Todo"];


[query countObjectsInBackgroundWithBlock:^(int number, NSError *error) {

    query.skip=arc4random_uniform(number-10);;
    query.limit=10;

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (error) {
        NSLog(@"An error occurred - %@",error.localizedDescription);
        }
        else {
            NSLog(@"object count=%d",objects.count);

            for (PFObject *object in objects) {
                NSLog(@"text=%@",object[@"text"]);
            }
        }
    }];


}];

      

+5


source


PFQuery

does not maintain random order, but you can work around this by creating an incrementing index field for each object

Then, given what you know maxIndex

about the table, you can generate random indexes like this:

- (NSArray *)generateRandomIndices:(int)maxIndex limit:(int)limit {
    NSMutableArray  *indices = [[NSMutableArray alloc] initWithCapacity:limit];
    for (int i=0; i<limit; i++) {
        int randomIndex  = arc4random() % maxIndex;
        [indices addObject:[NSNumber numberWithInt:randomIndex]];
    }
    return indices;
}

      



Now you can query your class using the IN

predicate

NSArray *randomIndices = [self generateRandomIndices:maxIndex limit:10];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"index IN %@", randomIndices];
PFQuery *query = [PFQuery queryWithClassName:@"className" predicate:predicate];

      

0


source


PFQuery gives no random objects. You can get all the objects and then randomize to get 10 objects from it and show them.

0


source







All Articles