简体   繁体   English

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

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

Basically i browsed stack overflow for a while to get a hang of AFNetworking framework. 基本上我浏览堆栈溢出一段时间以获得AFNetworking框架的挂起。 I decided to use AFHTTPClient, by making singleton class that extends AFHTTPClient. 我决定使用AFHTTPClient,通过制作扩展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. 这似乎没问题,如果你只是想发送JSON文件。 But i would like for my client to be more universal, so he can post pictures, and JSON to server. 但我希望我的客户能够更加通用,因此他可以将图片和JSON发布到服务器上。 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. 我正在使用AFHTTPClient的子类,我没有在实例中注册操作类。 You can find more information about the usage of registerHTTPOperationClass here . 您可以在此处找到有关registerHTTPOperationClass用法的更多信息。

I also recommend reading the comments in the AFNetworking source to understand the usage of the register operation class: 我还建议阅读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.

相关问题 使用iPhone中的AFNetworking下载队列中的文件 - Downloading Files in queue using AFNetworking in iPhone PHP没有使用AFNetworking从我的iPhone模拟器接收发布数据 - PHP not receiving post data from my iPhone simulator using AFNetworking 使用AFNetworking从iPhone向Web服务器发送pdf文件? - sending a pdf file from iphone to web server using AFNetworking? 使用具有不同值的同一个键传递多个参数+ AFnetworking - Pass multiple params with same key with different value + AFnetworking 是否保存或序列化AFNetworking操作队列? - Save or Serialize AFNetworking operations queue? 如何使用AFNetworking的enqueueBatchOfHTTPRequestOperations在操作失败时重新添加操作 - How to re-add operations when they fail using enqueueBatchOfHTTPRequestOperations from AFNetworking 在iPhone中使用不同的C函数访问同一对象? - Accessing Same object in different C function in iPhone? iPhone +相同的Xib适用于不同类别 - iphone+same xib for different class 是否可以使用同一项目在Xcode 4上生成两个不同的IPA? 一个用于iPhone,一个用于iPad? - Is it possible to generate two different IPAs on Xcode 4 using the same project? One for iPhone and one for iPad? 在同一个iPhone应用程序中使用两种不同的核心数据模型。 我该怎么做? - Using two different Core Data Models in the same iPhone app. How can I do it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM