gpt4 book ai didi

ios - 结合 AFHTTPRequestOperation 和 NSBlockOperation (iOS)?

转载 作者:行者123 更新时间:2023-11-29 02:30:51 36 4
gpt4 key购买 nike

我想执行 2 个请求并在最后发送通知。我检查操作顺序的代码:

AFHTTPRequestOperation *operation1 = ...;
AFHTTPRequestOperation *operation2 = ...;
[operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"asdf");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];
[operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

NSLog(@"asdf");
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {

}];
NSBlockOperation *operation3 = [NSBlockOperation blockOperationWithBlock:^{

NSLog(@"123");
}];
NSArray *operationArray = @[operation1, operation2, operation3];
for (int i = 1; i < operationArray.count; i++) {

NSOperation *op = operationArray[i];
[op addDependency:operationArray[i - 1]];
}
[queue addOperations:ops waitUntilFinished:NO];

我期待以下控制台日志:

asdf
asdf
123

但实际结果是:

asdf
123
asdf

为什么?

最佳答案

来自文档

The exact execution context for your completion block is not guaranteed but is typically a secondary thread. Therefore, you should not use this block to do any work that requires a very specific execution context. Instead, you should shunt that work to your application’s main thread or to the specific thread that is capable of doing it. For example, if you have a custom thread for coordinating the completion of the operation, you could use the completion block to ping that thread.

即不要依赖于在一个操作完成之后和另一个操作开始之前调用完成 block ,因为不能保证会那样发生。

关于ios - 结合 AFHTTPRequestOperation 和 NSBlockOperation (iOS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26895105/

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