Does AFNetworking automatically perform actions on a background thread?

为君一笑 提交于 2019-12-11 10:13:12

问题


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

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

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

- (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);
    }];
}

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.

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?


回答1:


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

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.

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



来源:https://stackoverflow.com/questions/28929993/does-afnetworking-automatically-perform-actions-on-a-background-thread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!