Search code examples
javascriptparse-platformbackground-processparse-cloud-code

Counter in background jobs


In the Parse Documentation on background jobs, there's an expression bothering me.

if (counter % 100 === 0) {
    // Set the  job's progress status
    status.message(counter + " users processed.");
  }
  counter += 1;

Can somebody explain this part? Especially the first line. Thanks! Also, what's the best way to call a job in Xamarin?

Parse.Cloud.job("userMigration", function(request, status) {
// Set up to modify user data
Parse.Cloud.useMasterKey();
var counter = 0;
// Query for all users
var query = new Parse.Query(Parse.User);
query.each(function(user) {
  // Update to plan value passed in
  user.set("plan", request.params.plan);
  if (counter % 100 === 0) {
    // Set the  job's progress status
    status.message(counter + " users processed.");
  }
  counter += 1;
  return user.save();
}).then(function() {
// Set the job's success status
status.success("Migration completed successfully.");
}, function(error) {
// Set the job's error status
  status.error("Uh oh, something went wrong.");
  });
});

Solution

  • As Rob pointed out, it's a modulo operation, a remainder in integer division. So (counter % 100 === 0) will be true when counter is 0, 100, 200, 300... They're doing it because they don't want to flood you with messages. Giving you a message for every hundredth user is enough.