如何批量更新User类(Parse.com,Android)中的所有对象?

时间:2016-08-08 08:58:51

标签: java android parse-platform parse-server

我最初犯了一个错误,忘了在用户注册时初始化我的数组字段(设计),所以在我的Parse仪表板中,行显示为(undefined),我希望它们在[] 。我必须为大约5000个用户执行此操作。我尝试了以下代码:

var query = new Parse.Query("User");
query.equalTo("designs", "(undefined)");
query.each(function(obj) {
  obj.set("designs", "[]");
  return obj.save();
}).then(function() {
  // All objects updated.
}, function(err) {
  console.log(err);
});

我认为(undefined)应该是别的东西,但我不确定是什么,因为它可能不是一个字符串。我该如何解决这个问题?

更新:第二次尝试。这种情况越来越近了吗?

    private void updateAllRows(final int skip) {

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.setLimit(500);
    query.setSkip(skip);
    query.whereDoesNotExist("designs");
    query.findInBackground(new FindCallback<ParseUser>() {

        @Override
        public void done(List<ParseUser> results, ParseException e) {
            if (results.size() > 0) {

                // The query was successful.
                for (int i = 0; i < results.size(); i++) {
                    ParseUser user = results.get(i);
                    String[] designs = new String[0];
                    user.put("designs", Arrays.asList(designs));

                    try {
                        ParseUser.saveAll(results);
                        if (results.size() >= 500) {
                            updateAllRows(skip + 500); // make a recursion call with different skip value
                        }
                    } catch (ParseException e1) {
                        e1.printStackTrace();
                    }
                }

                Log.d(getClass().toString(), ": The batch job was successfully completed.");

            } else {
                // The query was unsuccessful.
                Log.d(getClass().toString(), ": The batch job was not successful.");
            }
        }
    });
}

我在我的班级onCreate方法中用以下函数调用上面的函数:

int skip = 0;
updateAllRows(skip);

这是不使用云代码的最佳方法吗?

错误:

08-08 12:38:43.034 24003-24053/? E/HttpOperation: [luq{checkandengageuser, getmobileexperiments}] Unexpected exception
                                                  java.io.IOException: 1 of 2 operations in the batch failed
                                                      at luj.a(PG:268)
                                                      at lth.o(PG:10642)
                                                      at ltc.a(PG:1402)
                                                      at lth.i(PG:154)
                                                      at ly.f(PG:51849)
                                                      at dsr.a(PG:1255)
                                                      at dsr.a(PG:20077)
                                                      at dsr.a(PG:706)
                                                      at dss.run(PG:1548)
                                                      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
                                                      at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                      at java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:3)

JavaScript版 1.您可以使用Parse.Object.saveAll在一个服务调用中保存多个对象  2.您必须使用限制跳过才能一次为500(或更少)项目执行此操作。  3.您需要使用 query.doesNotExist(&#34; designs&#34;); 以获取设计列中包含undefined的所有项目

&#13;
&#13;
function updateAllRows(skip) {

  var query = new Parse.Query(Parse.User);
  query.limit(500);
  query.skip(skip);
  // get recors where designs is not exist or is undefined      
  query.doesNotExist("designs");

  query.find().then(function(results) {

    // if we got results then change the designs property 
    if (results.length > 0) {

      for (var i = 0; i < results.length; i++) {
        var item = results[i];
        item.set("designs", []);
      }

      Parse.Object.saveAll(results).then(function() {
        // if we got 500 or more results then we know
        // that we have more results
        // otherwise we finish
        if (results.length >= 500) {
          updateAllRows(skip + 500); // make a recursion call with different skip value
        }

      }, function(err) {
        // error occured 
      });
    }
  }, function(error) {
    // error occured while trying to fetch data 
  });
};
&#13;
&#13;
&#13;

Android Java版

private void updateAllRows(final int skip){

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.setLimit(500);
    query.setSkip(skip);
    query.whereDoesNotExist("designs");

    query.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(final List<ParseUser> users, ParseException e) {

            if (e == null){
                for (ParseUser user : users){
                    user.put("designs",new ArrayList<>());
                }

                // save all objects in one server call
                ParseObject.saveAllInBackground(users, new SaveCallback() {
                    @Override
                    public void done(ParseException e) {

                        if (e == null){
                            if (users.size() >= 500){
                                updateAllRows(skip + 500);
                            }
                        } else {
                            // error  while saving objects
                        }


                    }
                });
            }
        }
    });
}

}

BTW!您还可以在云代码中创建云功能并从客户端触发此功能

答案 1 :(得分:0)

Javascript 版本:

async updateAllRows(skip) {
    const query = new Parse.Query(Question);
    query.limit(500);
    query.skip(skip);
    query.doesNotExist("update");
    try {
      const results = await query.find();
      for (const result of results) {
        result.set('update', 'your update')
      }
      await Parse.Object.saveAll(results)
      if (results.length >= 500){
        updateAllRows(skip + 500).then().catch();
      }
    } catch (e) {
    }
  }