2015-03-08 52 views
1

有誰知道AFNetworking的調用是否會自動調度到後臺線程?AFNetworking是否會自動在後臺線程上執行操作?

例如,這裏就是我有...

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

在ServerAccessLayer,我派遣完成的代碼回主線。當我很久以前寫這段代碼時,出於某種原因,我的假設是當使用AFHTTPRequestOperationManager時,它會自動派發到後臺線程,所以我沒有實現它。現在,我似乎無法找到任何證據,並且我想知道我是否實際上沒有在後臺線程上調用任何網絡調用。

有人可以驗證AFHTTPRequestOperationManager是否自動在後臺線程上工作,還是需要自己處理?

+0

是的,它確實去回線程 – 2015-03-08 19:28:40

回答

3

AFNetwork是開源的,所以實際上你可以查看自己所有的代碼在Github上:https://github.com/AFNetworking/AFNetworking

看着AFHTTPRequestOperationManager,你可以看到,它會創建自己操作隊列時,它的初始化。所有的網絡請求都發生在這個操作隊列上。然後,默認情況下,將完成和失敗塊分派回主隊列。

總之,您的網絡操作將在後臺進行,但您的successfailure塊將位於主隊列中。

相關問題