I am calling a function that saves edits to a database and returns a dojo/Deferred
. I found that I could not save all records at once and so I limited each call to only send 150 records at a time and simply chained several calls together using a for
loop.
Every time this code runs, the first 150 records are successfully saved and the final batch of records are successfully saved. Any batches in between seem to be overwritten by the final batch.
Here is the code:
applyEdits : function(layer, adds, updates, deletes, editInterval) {
adds = adds || [];
updates = updates || [];
deletes = deletes || [];
var maxFeatures = Math.max(adds.length, updates.length, deletes.length);
var editInterval = editInterval || 155;
var deferred = new Deferred();
deferred.resolve();
for (var i = 0; i < maxFeatures; i+=editInterval) {
var addGroup = adds.slice(i, i+editInterval);
var updateGroup = updates.slice(i, i+editInterval);
var deleteGroup = deletes.slice(i, i+editInterval);
deferred = deferred.then(lang.hitch(this, function() {
return layer.applyEdits(addGroup, updateGroup, deleteGroup).then(function() {
console.log("success");
}, function(error) {
console.log(error);
});
}));
}
return deferred;
adds
, updates
, and deletes
are all Array
's of Object
's. My guess is that because Array
's store references, the references are being overwritten each time through the loop. I tried creating deep copies of the Array
's before passing them to layer.applyEdits
, but that had no effect.