gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:41:32 25 4
gpt4 key购买 nike

我有一个包含地址簿所有联系人的数组。当我在客户端和云代码中保存全部时,我的请求总是超时,导致部分保存联系人列表。这就是我想使用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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com