如何从Parse.com检索超过1000行?

时间:2013-06-22 03:12:41

标签: android listview parse-platform

我一直在使用Parse来检索列表视图的数据。不幸的是,他们默认将请求限制为100,最大为1000。在班上,我已经超过了1000。我在网上找到了一个链接,显示了在iOS上进行操作的方法,但是如何在Android上进行操作呢? Web Link

我目前正在循环中将所有数据添加到一个arraylist中,直到所有项目都完成(100)然后将它们添加到列表中

11 个答案:

答案 0 :(得分:43)

我已经想出如何实现我的目标:

声明全局变量

private static List<ParseObject>allObjects = new ArrayList<ParseObject>();

创建查询

final ParseQuery parseQuery = new ParseQuery("Objects");
parseQuery.setLimit(1000);
parseQuery.findInBackground(getAllObjects());

回调查询

int skip=0;
FindCallback getAllObjects(){
    return new FindCallback(){
        public void done(List<ParseObject> objects, ParseException e) {
            if (e == null) {

                allObjects.addAll(objects);
                 int limit =1000;
                if (objects.size() == limit){
                    skip = skip + limit;
                    ParseQuery query = new ParseQuery("Objects");
                    query.setSkip(skip);
                    query.setLimit(limit);
                    query.findInBackground(getAllObjects());
                }
                //We have a full PokeDex
                else {
                    //USE FULL DATA AS INTENDED
                }
        }
    };
}

答案 1 :(得分:14)

这是一个没有承诺的 JavaScript 版本..

这些是全局变量(集合不是必需的,只是我的坏习惯)..

   ///create a collection of cool things and instantiate it (globally)
    var CoolCollection = Parse.Collection.extend({
       model: CoolThing
    }), coolCollection = new CoolCollection();

这是&#34;循环&#34;获得结果的函数..

//recursive call, initial loopCount is 0 (we haven't looped yet)
function getAllRecords(loopCount){

    ///set your record limit
    var limit = 1000;

    ///create your eggstra-special query
     new Parse.Query(CoolThings)
            .limit(limit)
            .skip(limit * loopCount) //<-important
            .find({
             success: function (results) {
                 if(results.length > 0){

                     //we do stuff in here like "add items to a collection of cool things"
                     for(var j=0; j < results.length; j++){
                         coolCollection.add(results[j]);
                     }

                     loopCount++; //<--increment our loop because we are not done

                     getAllRecords(loopCount); //<--recurse
                 }
                 else
                 {
                     //our query has run out of steam, this else{} will be called one time only
                     coolCollection.each(function(coolThing){
                        //do something awesome with each of your cool things
                     });
                 }
            },
             error: function (error) {
                //badness with the find
             }
         });
}

这就是你如何称呼它(或者你可以用其他方式做到):

getAllRecords(0);

答案 2 :(得分:2)

JAVA

所以5年零4个月后,@ SquiresSquire的above answer需要进行一些更改以使其对我有用,我想与您分享

private static List<ParseObject>allObjects = new ArrayList<ParseObject>();

 ParseQuery<ParseObject> parseQuery = new ParseQuery<ParseObject>("CLASSNAME");
        parseQuery.setLimit(1000);
        parseQuery.findInBackground(getAllObjects());

FindCallback <ParseObject> getAllObjects() {
        return new FindCallback <ParseObject>() {
            @Override
            public void done(List<ParseObject> objects, ParseException e) {
                if (e == null) {
                   allObjects.addAll(objects);
                    int limit = 1000;
                    if (objects.size() == limit) {
                        skip = skip + limit;
                        ParseQuery query = new ParseQuery("CLASSNAME");
                        query.setSkip(skip);
                        query.setLimit(limit);
                        query.findInBackground(getAllObjects());
                    }
                    //We have a full PokeDex
                    else {
                        //USE FULL DATA AS INTENDED
                    }
                }
            }

        };

答案 3 :(得分:1)

C#中,我使用此递归:

private static async Task GetAll(int count = 0, int limit = 1000)
{
    if (count * limit != list.Count) return;
    var res = await ParseObject.GetQuery("Row").Limit(limit).Skip(list.Count).FindAsync();
    res.ToList().ForEach(x => list.Add(x));
    await GetAll(++count);
}

JS 版本:

function getAll(list) {
    new Parse.Query(Row).limit(1000).skip(list.length).find().then(function (result) {
        list = list.concat(result);
        if (result.length != 1000) {
            //do here something with the list...
            return;
        }

        getAll(list);
    });
}

用法:C#中的GetAll()和JS中的getAll([])

我将Row中的所有行存储在list中。在每个请求中,我获得1000行并跳过list的当前大小。当前导出的行数与预期行不同时,递归停止。

答案 4 :(得分:1)

YAS(又一个解决方案!)在 javascript 中使用async()await()

async parseFetchAll(collected = []) {
  let query = new Parse.Query(GameScore);
  const limit = 1000;

  query.limit(limit);
  query.skip(collected.length);

  const results = await query.find();

  if(results.length === limit) {
    return await parseFetchAll([ ...collected, ...results ]);
  } else {
    return collected.concat(results);
  }

}

答案 5 :(得分:1)

Swift 3 示例:

var users    = [String] ()
var payments = [String] ()
///set your record limit
let limit = 29
//recursive call, initial loopCount is 0 (we haven't looped yet)
func loadAllPaymentDetails(_ loopCount:  Int){
    ///create your NEW eggstra-special query
    let paymentsQuery  = Payments.query()
    paymentsQuery?.limit = limit
    paymentsQuery?.skip  = limit*loopCount
    paymentsQuery?.findObjectsInBackground(block: { (objects, error) in
        if let objects = objects {
            //print(#file.getClass(),"  ",#function,"  loopcount: ",loopCount,"      #ReturnedObjects: ",  objects.count)
            if objects.count > 0 {
                //print(#function, " no. of objects :", objects.count)
                for paymentsObject in objects {
                    let user   = paymentsObject[Utils.name] as! String
                    let amount = paymentsObject[Utils.amount] as! String
                    self.users.append(user)
                    self.payments.append(amount)
                }
                //recurse our loop with increment because we are not done
                self.loadAllPaymentDetails(loopCount + 1); //<--recurse
            }else {
                //our query has run out of steam, this else{} will be called one time only
                //if the Table had been initially empty, lets inform the user:
                if self.users.count == 1 {
                    Utils.createAlert(self, title: "No Payment has been made yet", message: "Please Encourage Users to make some Payments", buttonTitle: "Ok")
                }else {
                    self.tableView.reloadData()
                }
            }
        }else if error != nil {
            print(error!)
        }else {
            print("Unknown Error")
        }
    })
}

改编自@ deLux_247上面的示例。

答案 6 :(得分:1)

** 编辑:下面的答案是多余的,因为开放源代码解析服务器对要获取的最大行数没有任何限制

 //instead of var result = await query.find();
    query.limit(99999999999);//Any value greater then max rows you want
    var result = await query.find();**

原始答案:

Javascript /云代码

这是处理所有查询的一种干净方法

async function fetchAllIgnoringLimit(query,result) {
  const limit = 1000;
  query.limit(limit);
  query.skip(result.length);

  const results = await query.find();
  result = result.concat(results)

  if(results.length === limit) {
    return await fetchAllIgnoringLimit(query,result );
  } else {
    return result;
  }

}

这是使用方法

var GameScore = Parse.Object.extend("GameScore");
var query = new Parse.Query(GameScore);
//instead of var result = await query.find();
var result = await fetchAllIgnoringLimit(query,new Array());
console.log("got "+result.length+" rows")

答案 7 :(得分:1)

重要提示如果您使用开放式,则此处的答案均无用 源解析服务器,默认情况下它确实限制100行,但是您可以 在查询中放入任何值,limit(100000)// WORKS 无需递归 呼叫只是将限制限制为所需的行数

https://github.com/parse-community/parse-server/issues/5383

答案 8 :(得分:0)

您可以使用CloudCode实现这一目标...创建一个可以调用的自定义函数,它将枚举整个集合并从中构建响应,但更明智的选择是对您的请求进行分页,并获取记录1000(甚至是(一次),根据需要动态地将它们添加到列表中。

答案 9 :(得分:0)

SWIFT 4的通用版本:

警告:这未经测试!

尝试使nyxee的答案适用于任何查询:

func getAllRecords(for query: PFQuery<PFObject>, then doThis: @escaping (_ objects: [PFObject]?, _ error: Error?)->Void) {
    let limit = 1000
    var objectArray : [PFObject] = []
    query.limit = limit
    func recursiveQuery(_ loopCount:  Int = 0){
        query.skip  = limit * loopCount
        query.findObjectsInBackground(block: { (objects, error) in
            if let objects = objects {
                objectArray.append(contentsOf: objects)
                if objects.count == limit {
                    recursiveQuery(loopCount + 1)
                } else {
                    doThis(objectArray, error)
                }
            } else {
                doThis(objects, error)
            }
        })
    }
    recursiveQuery()
}

答案 10 :(得分:0)

这是我的 C# .NET 解决方案

 List<ParseObject> allObjects = new List<ParseObject>();
 ParseQuery<ParseObject> query1 = ParseObject.GetQuery("Class");
 int totalctr = await query1.CountAsync()
 for (int i = 0; i <= totalctr / 1000; i++)
 {
     ParseQuery<ParseObject> query2 = ParseObject.GetQuery("Class").Skip(i * 1000).Limit(1000);
     IEnumerable<ParseObject> ibatch = await query2.FindAsync();
     allObjects.AddRange(ibatch);
 }