简体   繁体   中英

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

Basically i browsed stack overflow for a while to get a hang of AFNetworking framework. I decided to use AFHTTPClient, by making singleton class that extends AFHTTPClient. Some of the code that I have seen goes like this:

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

I have noticed that when creating new instance of client, you must register operation class. And that seems ok, if you just wonna send JSON files. But i would like for my client to be more universal, so he can post pictures, and JSON to server. For this do I need to un register operation class and register new class?

I'm using a subclass of AFHTTPClient and I don't register the operation class in the instance. You can find more information about the usage of registerHTTPOperationClass here .

I also recommend reading the comments in the AFNetworking source to understand the usage of the register operation class:

/**
 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.
 */

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM