Getting an arbitrarily large number of objects in the analysis

In my Parse database, I have a "Connection" class that unites two users. In one of my methods, I try to find all the users that the person is connected to. However, I am stuck between two different options, none of which seem very effective.

My first reaction was to constantly query db until all objects were returned (for example, "query first 1000, query second 1000, ..."). In most cases, this would be ideal, as most users would have less than 1000 connections (probably more than 2-300 or so). However, if a user has an insane amount of connections, this will not work too well, especially because Pars has break limits.

Another option seems to be to use the Query.each method to simply iterate over all the records matching the query. I believe that this will work regardless of the number of elements, so this is good. However, it seems like it's relatively slow for large sizes and probably a wait time.

So what would be the best way to do this with Parse restrictions? I want this to be fast in the general case of a relatively small number of objects, but of course it should not break for edge cases. Of course, one of the options would be to simply not fulfill this type of request, but it is very useful to have all connections on the client side. Thank!

+4
source share
1 answer

Great question, and I found a good solution for my work. The fastest and most efficient workaround method is to query the number of objects, and if it more than 1000 performs several queries in the same table, the property of the skip request is increased by 1000 each time:

[query countObjectsInBackgroundWithBlock] "skip". 1000 , . , .

, , , .

- :

//Define these as class-wide variables in your @interface - don't forget to @synthesize them too!
int queriesToComplete;
int completedQueries;
NSMutableArray *completeDataArray = [[NSMutableArray alloc] init];

//Put in your query load function
PFQuery *recordsCountQuery = [PFQuery queryWithClassName:@"YourClassName"];
[recordsCountQuery countObjectsInBackgroundWithBlock:^(int number, NSError *error) {
    //Determine how many queries to run
    queriesToComplete = (int)ceil(number/1000) + 1;
    //Set the number of completed queries to zero
    completedQueries = 0;
    //Run a chain of queries - be sure to run them in the background to prevent UI lockup
    for (int i = 0; i < queriesToComplete; i++) {
        PFQuery *chainQuery = [PFQuery queryWithClassName:@"YourClassName"];
        //Add your other 'whereKey: equalTo:' type qualifiers here
        chainQuery.limit = 1000;
        chainQuery.skip = (i*1000);
        [chainQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            //NSLog(@"query done");
            [completeDataArray addObjectsFromArray:[objects copy]];
            completedQueries ++;
            [self pullCompletionCheck];
        }];
    }
}];

- (void)pullCompletionCheck{
    if (self.queriesToComplete == self.completedQueries) {
        //All queries completed.  
        //All data is now in the completeDataArray

    }
}
+5

All Articles