简体   繁体   中英

How can I batch update all objects in User class (Parse.com, Android)?

I made a mistake initially and forgot to initialize my Array field (designs) upon user registration, so in my Parse dashboard the rows show up as (undefined) , where I would prefer them to be [] . I have to do this for about 5000 users. I tried the following code:

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);
});

I think (undefined) should be something else, but I'm not sure what, as it's probably not a String. How can I solve this?

Update: A second attempt. Is this getting closer?

    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.");
            }
        }
    });
}

I call teh above function with the following from my class's onCreate method:

int skip = 0;
updateAllRows(skip);

Is this the best way to go about this without using Cloud code?

Error:

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)

JavaScript Version 1. You can use Parse.Object.saveAll in order to save multiple objects in one service call 2. You must use limit and skip in order to do it for 500 (or less) items at a time. 3. You need to use query.doesNotExist("designs"); in order to get all items which contain undefined in the design column

 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 }); }; 

Android Java Version

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! you can also create a cloud function in cloud code and trigger this function from the client

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