簡體   English   中英

(iPhone)AFNetworking使用相同的共享客戶端進行不同的操作?

[英](iPhone) AFNetworking Using same shared client for different operations?

基本上我瀏覽堆棧溢出一段時間以獲得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發布到服務器上。 為此,我需要取消注冊操作類並注冊新類嗎?

我正在使用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.
 */

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM