Verifying uniqueness on a batch save with a huge amount of objects - Parse Performance

妖精的绣舞 提交于 2019-12-13 08:14:46

问题


I'm doing a parse batch save requestas follows:

Parse.Object.saveAll(nameGenderArrayToSave)

nameGenderArrayToSave is an array with thousands of objects to save. I'm also interested in guarante the uniqueness of my data. So I have a beforeSave hook that do it for me:

Parse.Cloud.beforeSave("NameGender", function(request, response) {
    if (!request.object.isNew()) {
      // Let existing object updates go through
      response.success();
    }
    var query = new Parse.Query("NameGender");
    // Add query filters to check for uniqueness
    query.equalTo("name", request.object.get("name"));
    query.first().then(function(existingObject) {
      if (existingObject) {
        // Update existing object
        if (request.object.get("gender") != "U"){
            existingObject.set("gender", request.object.get("gender"));
        }
        return existingObject.save();
      } else {
        // Pass a flag that this is not an existing object
        return Parse.Promise.as(false);
      }
    }).then(function(existingObject) {
      if (existingObject) {
        // Existing object, stop initial save
        response.error("Existing object");
      } else {
        // New object, let the save go through
        response.success();
      }
    }, function (error) {
      response.error(error);
    });
});

The code above is running as expected but i'm with a performance problem since i'm triggering a find for each object i'm saving and so i'm reaching Parse request/sec limit. Could anyone help me here finding a way to solve this?

来源:https://stackoverflow.com/questions/33762391/verifying-uniqueness-on-a-batch-save-with-a-huge-amount-of-objects-parse-perfo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!