简体   繁体   English

如何在课堂上获取Parse.com对象计数

[英]How to get Parse.com object count in class

I want to get the object count from 5 classes in Parse.com (to check if all objects were fetched successfully). 我想从Parse.com中的5个类中获取对象计数(以检查是否成功获取了所有对象)。

Because I'm using findObjectsInBackgroundWithBlock : sometimes not all the objects are fetched before I'm using it, that's why I want to check. 因为我使用的是findObjectsInBackgroundWithBlock :有时在使用它之前并非所有对象都已获取,所以这就是为什么要检查的原因。

How can I do that? 我怎样才能做到这一点?

Update 2 : just noticed you were asking about iOS. 更新2 :刚注意到您在问有关iOS的问题。 its basically the same principle, use fetchAllIfNeeded like so: https://parse.com/docs/ios/api/Categories/PFObject(Synchronous).html#/c:objc(cs)PFObject(cm)fetchAllIfNeeded : 其基本相同的原理,使用fetchAllIfNeeded就像这样: https ://parse.com/docs/ios/api/Categories/PFObject(Synchronous).html#/c:objc(cs)PFObject(cm)fetchAllIfNeeded:

Update : a better way than the naive one (below) would probably be using fetchAllIfNeededInBackground: 更新 :比天真(下面)更好的方法可能是使用fetchAllIfNeededInBackground:

ArrayList<ParseObject> objectsToFetch = new ArrayList<>();
objectsToFetch.add(object1);
objectsToFetch.add(object2);
objectsToFetch.add(object3);
ParseObject.fetchAllIfNeededInBackground(objectsToFetch, new FindCallback<ParseObject>() {
    @Override
    public void done(List<ParseObject> objects, ParseException e) {
        //all are fetched, do stuff
    }
});

My native way of doing this is adding an outer boolean array where each boolean is responsible for one of the classes. 我的本机操作方式是添加一个外部布尔数组,其中每个布尔值负责其中一个类。 When a findObjectsInBackgroundWithBlock's 'done' function runs, I set that boolean to "true" and run a function that checks whether all array is true, if so -> all classes have been fetched and I can continue. 当findObjectsInBackgroundWithBlock的'done'函数运行时,我将该布尔值设置为“ true”并运行一个检查所有数组是否为true的函数,如果是,则->所有类均已获取,我可以继续。

example from my code: 我的代码示例:

boolean[] itemFetched;

protected void getAllClassesAndDoStuffWithThem() {
    itemFetched= new boolean[NUM_OF_CLASSES];

    for (int i=0;i<NUM_OF_CLASSES;i++){
        final int finalI = i;
        itemFetched[i] = false;
        parseObjectArray[i].fetchIfNeededInBackground(new GetCallback<ParseObject>() {
            @Override
            public void done(ParseObject object, ParseException e) {
                itemFetched[finalI] = true;
                finishInitIfPossible();
            }
        });
    }


}

private void finishInitIfPossible() {
    for (int i=0;i<NUM_OF_CLASSES;i++){
        if (!itemFetched[i])
            return;
    }

    //all classes fetched
    finishInit();
}

private void finishInit() {
    //do stuff with all 5 classes
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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