ios - 解析.com : Pass an array of dictionaries to backgroundJob through httprequest

标签 ios objective-c cloud parse-platform

我有一个包含地址簿所有联系人的数组。当我在客户端和云代码中保存全部时,我的请求总是超时,导致部分保存联系人列表。这就是我想使用cloudcode backgroundjob的原因。

我找不到将数组传递给后台作业的方法。我该怎么做?

iOS 客户端调用 httpRequest 并传递 allItems

 [PFCloud callFunctionInBackground:@"httpRequest" withParameters:@{@"myArray" :allItems} block:^(NSString *result, NSError *error) {
        if (!error) {
            NSLog(@"Result is: %@", result);
        }
    }];

我的http请求

Parse.Cloud.define('httpRequest', function(request, response) {
  var body =  request.params.myArray;

  Parse.Cloud.httpRequest({
      method: "POST",
      url: "https://api.parse.com/1/jobs/userMigration",
      headers: {
        "X-Parse-Application-Id": "...",
         "X-Parse-Master-Key": "...",
         "Content-Type": "application/json"
      },
      body:
        "body" : body;
      ,
      success: function(httpResponse) {
        console.log(httpResponse);
      },
      error: function(error) {
        console.log("ERROR"); 
      }
    });
});

我的后台工作功能。 (这个函数在用作基本的云代码函数时工作正常)

Parse.Cloud.job("userMigration", function(request, status) {
  // Set up to modify user data
  var array = request.params.myArray;
  var arr = new Array();
  var user = request.user;

  array.forEach(function(entry) {
      var Contact = Parse.Object.extend("Contacts");
      var contact = new Contact();
      contact.set("name", entry.name);
      contact.set("email", entry.email);
      contact.set("phone", entry.phone);
      contact.set("phoneFormated", entry.phoneFormated);
      contact.set("userId", user);
      arr.push(contact);
  });
    Parse.Object.saveAll(arr, {
        success: function(objs) {
            // objects have been saved...
            reponse.success("object saved")
        },
        error: function(error) { 
            // an error occurred...
            response.error("mistake")
        }
    });
});

最佳答案

您是否尝试过将数组作为 JSON 传递?喜欢:

...
      body: JSON.stringify(body),
...

在后台工作:

  var array = JSON.parse(request.body);

另外请注意,您的云函数将等待后台作业的响应,因此您的云函数仍有可能超时(但我不确定后台作业是否会被杀死)。

如果您在 parse.com 中有免费计划,请不要忘记您只能同时运行一个后台作业。这就是为什么从您的 iOS 应用程序调用后台作业不是一个好的解决方案。也许您应该尝试直接从 parse.com 安排后台作业或调用多个云函数/保存。

我认为最适合您的解决方案是这样的:

NSUInteger arraySize = 10;
__block NSUInteger callCount = array.length / arraySize; // the number of call to saveAll you will make

for (NSUInteger i = 0 ; i < array.length / arraySize ; ++i) {
    NSArray * tmpArray = [allItems subarrayWithRange:NSMakeRange(i * arraySize, arraySize)];

    [PFObject saveAllInBackground:tmpArray block:^(BOOL succeeded, NSError *error) {
        callCount--;
        if (succeeded) {
            if (callCount < 0) {
                NSLog(@"last item has been saved");
            }
        } else {
            // handle error
        }
    }];
}

关于ios - 解析.com : Pass an array of dictionaries to backgroundJob through httprequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24325431/

相关文章:

ios - 在 React Native 前台设置 PushNotificationIOS 时发出警告

iphone - 触摸移动时拖动图像变慢

cloud - jclouds 支持更新的 vCloud API

amazon-web-services - 顺序事件的 AWS SWF 良好实践

iOS 内存效率和性能 : Pass Data via Segue or Create New Instance?

ios - 代码 :Get Data From Database device

ios - 在 UITableView 上长按手势

ios - 无法更改自定义 UITableViewCell 中 UILabel 的大小

ios - "A GKScore must specify a leaderboard."

asp.net - 指向 SQL Azure 的 Azure 云服务 Web 角色存在问题