Search code examples
javaandroidparse-platformparse-server

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)

Solution

  • i just correnct to above answer, the param skip stay at 0, cause data has updated

    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); // make a recursion call with different skip value
        }
    
      }, function(err) {
        // error occured 
      });
    }
    }, function(error) {
    // error occured while trying to fetch data
    });
    };
    

    Simulation, have 3850 data for update

    0 ->TEST 1000 0 ->TEST 1000 0 ->TEST 1000 0 ->TEST 850