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

前端 未结 3 462
离开以前
离开以前 2021-01-25 00:56

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

3条回答
  •  佛祖请我去吃肉
    2021-01-25 01:25

    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

    In Android,

        HashMap params = new HashMap();
        params.put("movie", "The Matrix");
    
        ParseCloud.callFunctionInBackground("averageStars", params, new
           FunctionCallback() {
          void done(ParseObject ratings, ParseException e) {
           if (e == null) {
              // Do your stuff
              }
          }
       });
    

    Thanks to st.derrick for biggest hint.

提交回复
热议问题