0

基本上我浏览了一段时间的堆栈溢出来了解 AFNetworking 框架。我决定使用 AFHTTPClient,通过制作扩展 AFHTTPClient 的单例类。我见过的一些代码是这样的:

 (InspectionClient*) sharedClient {

static InspectionClient *client = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    client = [[InspectionClient alloc] initWithBaseURL: [NSURL URLWithString:kServerName]];
});

return client;

}

- (id) initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {

    // register operation class
    [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
return self;
}

我注意到在创建新的客户端实例时,您必须注册操作类。如果您只是不想发送 JSON 文件,那似乎没问题。但我希望我的客户更通用,所以他可以将图片和 JSON 发布到服务器。为此我需要取消注册操作类并注册新类吗?

4

1 回答 1

2

我正在使用的子类,AFHTTPClient并且我没有在实例中注册操作类。registerHTTPOperationClass 您可以在此处找到有关使用的更多信息 。

我也推荐阅读源码中的注释,AFNetworking了解寄存器操作类的用法:

/**
 Attempts to register a subclass of `AFHTTPRequestOperation`, 
 adding it to a chain to automatically generate request operations from a 
 URL request.

 @param operationClass The subclass of `AFHTTPRequestOperation` to register

 @return `YES` if the registration is successful, `NO` otherwise. 
 The only failure condition is if `operationClass` is not a subclass of
  `AFHTTPRequestOperation`.

 @discussion When `enqueueHTTPRequestOperationWithRequest:success:failure` 
 is invoked, each registered class is consulted in turn to see if it can 
 handle the specific request. The first class to return `YES` when sent a
 `canProcessRequest:` message is used to create an operation using 
 `initWithURLRequest:` and do `setCompletionBlockWithSuccess:failure:`. 
 There is no guarantee that all registered classes will be consulted. 
 Classes are consulted in the reverse order of their registration. 
 Attempting to register an already-registered class will move it to the 
 top of the list.
 */
于 2012-12-13T14:30:46.193 回答