gpt4 book ai didi

iOS - 如何使用循环增加 block 中的变量

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

我需要在服务器上创建一个实体,然后将几张图片上传到服务器。

所以首先阻止显示成功在服务器上创建实体然后我开始循环上传 10 张图片,但是应用程序不会在最后 10 张图片上传后发送通知,所以 'i' 变量可以是 10 甚至不是订单中的 10。我不确定,但似乎 block 中的迭代不正确。所以我只想确保这 ​​10 张图片已上传,然后调用发送通知。

所以我跳过了一些 block 参数和失败选项、我用于获取要上传的图像的数组等。只需将我的 block 作为在“{”之后显示成功调用的示例。

// success first block
block1
{
// cycle from 0 to 10
for (NSInteger i = 0; i <=10; i++)
{
// success upload image to the server block
block2
{
// if I did 10 uploads I need to send notification.
if (i == 10)
{
// send notification here when last block returned success....
}
}
}
}

最佳答案

如果您正在创建一堆 AFHTTPRequestOperation 对象,那么最合乎逻辑的方法是创建一个完成操作并使其依赖于请求操作:

NSOperation *completionOperation = [NSBlockOperation blockOperationWithBlock:^{
// code to be run when the operations are all done should go here
}];

for (...) {
// create `operation` AFHTTPRequestOperation however you want and add it to some queue

// but just make sure to designate the completion operation dependency

[completionOperation addDependency:operation];
}

// now that all the other operations have been queued, you can now add the completion operation to whatever queue you want

[[NSOperationQueue mainQueue] addOperation:completionOperation];

关于iOS - 如何使用循环增加 block 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441788/

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