【问题标题】:Update many objects at once in Parse在 Parse 中一次更新多个对象
【发布时间】:2015-07-13 05:10:40
【问题描述】:

我需要一次更新一堆对象,但由于文档建议调用.getObjectInBackgroundWithID,因此我找不到一种有效的方法来一次完成所有操作。我没有每个对象的 ID,即使有我也无法将它们全部传递进去。

问题: 1)在云代码中调用这个函数比在客户端处理这一切更有意义,对吧? 2) 在 JS (Cloud Code)/Swift 的 for 循环中更新多个具有相同值的对象的最佳方法是什么?

【问题讨论】:

    标签: javascript ios swift parse-platform parse-cloud-code


    【解决方案1】:

    我认为您正在寻找带有.findObjects(及其变体)的查询,然后使用PFObject 的类方法.saveAll(及其变体)来保存对象数组。 p>

    这是sample

    var query = PFQuery(className:"GameScore")
    query.whereKey("playerName", equalTo:"Sean Plott")
    query.findObjectsInBackgroundWithBlock {
      (objects: [AnyObject]?, error: NSError?) -> Void in
    
      if error == nil {
        // The find succeeded.
        println("Successfully retrieved \(objects!.count) scores.")
        // Do something with the found objects
        if let objects = objects as? [PFObject] {
          for object in objects {
            println(object.objectId)
            // Do your manipulation
          }
    
          // Save your changes to ALL objects
          PFObject.saveAllInBackground(objects, block: {
            (succeeded: Bool, error: NSError!) -> Void in
    
            if (error == nil) {
                println("Successfully saved \(objects!.count) objects.")
            } else {
                // Log details of the failure
                println("Error: \(error!) \(error!.userInfo!)")
          })
        }
      } else {
        // Log details of the failure
        println("Error: \(error!) \(error!.userInfo!)")
      }
    }
    

    CloudCode sample:

    var GameScore = Parse.Object.extend("GameScore");
    var query = new Parse.Query(GameScore);
    query.equalTo("playerName", "Dan Stemkoski");
    query.find({
      success: function(results) {
        alert("Successfully retrieved " + results.length + " scores.");
        // Do something with the returned Parse.Object values
        for (var i = 0; i < results.length; i++) {
          var object = results[i];
          alert(object.id + ' - ' + object.get('playerName'));
        }
      },
      error: function(error) {
        alert("Error: " + error.code + " " + error.message);
      }
    });
    

    【讨论】:

    • 我知道.findObjectsInBackground,但我无法在for-in 循环中更新和保存这些对象。那是我的问题。我需要查询一个类,并更新我在 for 循环中得到的每个返回的对象。
    • 您可以获取所有数据,在.findObjectsInBackground 块中更新它们,然后调用PFObject 的类方法.saveAllInBackground(请参阅编辑后的答案)。
    • 如何在 Cloud Code 中做到这一点,这样我就不必处理来自客户端的所有额外带宽?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多