2012-12-26 75 views
2

我試圖保存Cloud代碼函數中的對象集合。Parse.com Cloud代碼 - saveAll請求超時

30-40個對象後,我得到一個超時錯誤。我的代碼如下所示:

Parse.Cloud.define("saveInBackground", function (request, response) { 
    console.log("saveInBackground begin"); 

    var objectsToSave = []; 

    for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) { 
     objectsToSave.push(new LikedObject(request.params.collectionToSave[i])); 
    }; 

    Parse.Object.saveAll(objectsToSave, { 
     success: function(list) { 
      // All the objects were saved. 
      if (response) { 
       response.success(list); 
      }; 

      console.log("saveInBackground success"); 
     }, 
     error: function(model, error) { 
      // An error occurred while saving one of the objects. 
      if (response) { 
       response.error(error); 
      }; 

      console.log("saveInBackground error: " + error.message); 
     } 
    }); 

    console.log("saveInBackground end"); 
}); 

我可以做其他事情來保存一堆對象嗎?

回答

4

根據您的對象的大小,您可以嘗試將它們分批保存到20-30。這是必需的,因爲saveAll()正在嘗試上傳您提供給服務器的所有對象。以下是你如何做的示例代碼:

var result = true; 
for (var i = request.params.collectionToSave.length - 1; i >= 0; i--) { 
    objectsToSave.push(new LikedObject(request.params.collectionToSave[i])); 
    if (i % 10 == 0) { 
     result = saveObjects(objectsToSave); 
     objectsToSave.length = 0; 
    } 
}; 
if (result == true) { 
    console.log("saveInBackground success"); 
} 


function saveObjects(objects) { 

    Parse.Object.saveAll(...); 
    ... 
}