Icaro
Icaro

Reputation: 14845

Many request to parse at once

I have a list of request to make to Parse.com using the swif API for tasks acumulated once the application was offine. Some tests show that if I dowload it all at once the overall time is slow that is I use multiple requests. However I couldn't figureout how can I request many "random" objectId's from Pase.com (I have a list of course, by random I mean out of order and not a fix number)

At the moment I am using a loop and calling many:

 let pred = NSPredicate(format: "newDataID = %@, dataID[i])
 query.findObjectsInBackgroundWithBlock { (result:[AnyObject]?, error:NSError?)

I was thinking in auto generate the string for the predicate but it can get very long what I image would make the query very slow.

Any ideas?

Upvotes: 0

Views: 43

Answers (1)

danh
danh

Reputation: 62676

Under any circumstances, initiating many requests in a tight loop is ill-advised. Instead, send the dataID array to a cloud function. Also, if its really an array of object ids, then find is the wrong method, use get() instead...

var _ = require('underscore');  // underscore includes many handy functions, including map and toArray

Parse.Cloud.define("getManyObjectsById", function(request, response) {
    var dataID = request.params.dataID;
    var promises = _.map(dataID, function(anID) {
        var query = new Parse.Query("MyCustomClassName");
        return query.get(anID);
    });
    Parse.Promise.when(promises).then(function() {
        response.success(_.toArray(arguments));
    }, function(error) {
        response.error(error);
    });
});

Call it...

PFCloud.callFunctionInBackground("getManyObjectsById", withParameters: dataID) {
  (objects: [AnyObject]?, error: NSError?) -> Void in
      // objects should be an array of objects corresponding to the ids
}

Upvotes: 2

Related Questions