AFNetworking是否自动在后台线程上执行操作? [英] Does AFNetworking automatically perform actions on a background thread?

查看:324
本文介绍了AFNetworking是否自动在后台线程上执行操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道AFNetworking的呼叫是否自动调度到后台线程吗?

Does anyone know if AFNetworking's calls automatically dispatch to a background thread?

例如,这就是我所拥有的...

For example, here's what I have...

ServerAccessLayer.m

ServerAccessLayer.m

- (void)fetchWithurlString:(NSString *)urlString andCompletion:(APIResponseObjectBlock)completion
{
    [[WebClient sharedClient] fetchDictionaryWithURLString:urlString success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (completion) {
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(YES, responseObject, operation.response.statusCode, nil, nil);
            });
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error, NSInteger statusCode, id responseObject) {
        if (completion) {
            NSArray *messages = [self generateResponseArray:responseObject withDataType:MESSAGE];
            NSArray *errors = [self generateResponseArray:responseObject withDataType:ERROR];
            dispatch_async(dispatch_get_main_queue(), ^{
                completion(NO, responseObject, statusCode, messages, errors);
            });
        }
    }];
}

WebClient.m

WebClient.m

- (void)fetchDictionaryWithURLString:(NSString *)urlString success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *, NSInteger, id))failure
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:urlString parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        success(operation, responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        failure(operation, error, ((NSHTTPURLResponse *)operation.response).statusCode, operation.responseObject);
    }];
}

在ServerAccessLayer中,我将完成代码分派回主线程.很久以前,当我编写此代码时,出于某种原因,我的假设是,当使用AFHTTPRequestOperationManager时,它会自动在后台线程上分派,因此我没有实现它.现在,我似乎找不到任何证据,我想知道我是否实际上没有在后台线程上调用我的任何网络调用.

In ServerAccessLayer, I dispatch the completion code back on the main thread. When I wrote this code long ago, my assumption for some reason was that when using AFHTTPRequestOperationManager, it automatically dispatched on a background thread so I did not implement it. Now, I don't seem to be able to find any evidence of that, and I'm wondering if I am in fact not calling any of my networking calls on the background thread.

有人可以为我验证AFHTTPRequestOperationManager是否自动在后台线程上运行,还是我需要自己处理?

Can someone verify for me if AFHTTPRequestOperationManager automatically does it's work on a background thread, or do I need to be handling this myself?

推荐答案

AFNetwork是开源的,因此您实际上可以在Github上自己查看所有代码:

AFNetwork is open source, so you can actually view all the code for yourself on Github: https://github.com/AFNetworking/AFNetworking

看着AFHTTPRequestOperationManager,您可以看到它在初始化时创建了自己的操作队列.所有网络请求都在此操作队列上发生.然后,默认情况下,完成和失败块将分派回主队列.

Looking at AFHTTPRequestOperationManager, you can see that it creates its own operation queue when it's initialized. All network requests take place on this operation queue. Then, by default, the completion and failure blocks are dispatched back onto the main queue.

简而言之,您的网络操作将在后台进行,但是您的successfailure块将位于主队列中.

In short, your network operations will take place in the background, but your success and failure blocks will be on the main queue.

这篇关于AFNetworking是否自动在后台线程上执行操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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