是否在主线程上调用了AFNetworking成功/失败块? [英] Are AFNetworking success/failure blocks invoked on the main thread?

查看:321
本文介绍了是否在主线程上调用了AFNetworking成功/失败块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

AFNetworking是否在主线程上调用完成块?或者是在后台调用,要求我手动将我的UI更新发送到主线程?

Does AFNetworking call the completion block on the main thread? or is it called in the background, requiring me to manually dispatch my UI updates to the main thread?

使用代码而不是单词,这是<的示例代码a href =https://github.com/AFNetworking/AFNetworking> AFNetworking文档,用UI更新替换 NSLog 的调用:

Using code instead of words, this is the example code from the AFNetworking documentation with the call to NSLog replaced by a UI update:

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    self.label.text = JSON[@"text"];
} failure:nil];

是否应该这样写?

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    dispatch_async(dispatch_get_main_queue(), ^{
        self.label.text = JSON[@"text"];
    });
} failure:nil];


推荐答案

在AFNetworking 2中, AFHTTPRequestOperationManager 有一个 completionQueue property。

In AFNetworking 2, AFHTTPRequestOperationManager has a completionQueue property.


completionBlock的调度队列请求操作。
如果 NULL (默认),则使用主队列。

The dispatch queue for the completionBlock of request operations. If NULL (default), the main queue is used.



    #if OS_OBJECT_USE_OBJC
    @property (nonatomic, strong, nullable) dispatch_queue_t completionQueue;
    #else
    @property (nonatomic, assign, nullable) dispatch_queue_t completionQueue;
    #endif

在AFNetworking 3中, completionQueue 属性已移至 AFURLSessionManager AFHTTPSessionManager 延伸)。

In AFNetworking 3, the completionQueue property has been moved to AFURLSessionManager (which AFHTTPSessionManager extends).


completionBlock 的调度队列。如果 NULL (默认),则使用
主队列。

The dispatch queue for completionBlock. If NULL (default), the main queue is used.



@property (nonatomic, strong) dispatch_queue_t completionQueue;
@property (nonatomic, strong, nullable) dispatch_queue_t completionQueue;

这篇关于是否在主线程上调用了AFNetworking成功/失败块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆