简体   繁体   中英

how to get random rows(parse objects) from parse table using android parse sdk?

I have 500 records on parse class or table and now i need to get 10 random records out of 500 records?

Please tell me how can I do this.

 ParseQuery<ParseObject> query = ParseQuery.getQuery(parseTableName);
 query.setLimit(10);
 query.findInBackground(new FindCallback<ParseObject>() {
     @Override
     public void done(List<ParseObject> parseObjects,com.parse.ParseException e) {
     }
 }

The best way is probably to write a CloudCode module that downloads 500 objects, then randomly selects 10 to send down to your Android app in the response. That's much better than downloading 500 objects to your device and choosing 10.

It's been a while since I've written CloudCode, but you could do something like the following.

in iOS app (you can do a little work to find the Android equivalent):

[PFCloud callFunctionInBackground:@"get500obj" withParameters:@{} block:^(id result, NSError *error) {
   // do something with result
}];

in CloudCode (this should be treated as pseudocode as it's untested):

Parse.Cloud.define('get500obj', function(request, response) {
    // for getting random element
    Array.prototype.randomElement = function () {
        return this[Math.floor(Math.random() * this.length)]
    }

    var query = new Parse.Query("your-object-class-name");
    query.find({
        success: function(results) {
            var final10 = [];
            for (var i = 0; i < 10; i++) {
                var myRandomElement = results.randomElement()
                if (final10.indexOf(myRandomElement) == -1) {
                    final10.push(myRandomElement);
                } else {
                    i--;
                }
            }
            response.success(final10);
        },
        error: function() {
            response.error(error);
        }
    });
});

Here is an Objective C code sample.

PFQuery *query = [PFQuery queryWithClassName:@"MyTable"];
int count = [query countObjects];

int r = arc4random_uniform(count);

PFQuery *query2 = [PFQuery queryWithClassName:@"MyTable"];
query2.skip = r;

PFObject *result = [query2 getFirstObject];

`

This is working code for fetching random objects from given class in Parse

In cloud code,

Parse.Cloud.define("get10Obj",function(request,response)
{ 

query = new Parse.Query(request.params.movie);

Array.prototype.randomElement = function (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}


query.find ({

success: function(results) {

        var final10 = [];

        for (var i = 0; i < 5; i++) {
            var myRandomElement = results.randomElement(0,results.length);

            if (final10.indexOf(myRandomElement) == -1) {
                final10.push(myRandomElement);
            } else {
                i--;
            }
        }
         var datalist =[];

        for(var j=0;j<final10.length;j++)
        {
            var index= final10[j];
            datalist.push(results[index]);
        }   

        response.success(datalist);
    },
    error: function() {
        response.error(error);
      }
    });
  });

In Android,

    HashMap<String, Object> params = new HashMap<String, Object>();
    params.put("movie", "The Matrix");

    ParseCloud.callFunctionInBackground("averageStars", params, new
       FunctionCallback<ParseObject>() {
      void done(ParseObject ratings, ParseException e) {
       if (e == null) {
          // Do your stuff
          }
      }
   });

Thanks to st.derrick for biggest hint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM