I am trying to understand how the AFNetworking
Framework works. But there is little detail I do not understand.
I wrote a subclass of AFHTTPclient
than made it a singleton class and added a Initializer that does the following :
- (id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (self) {
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
}
return self;
}
I got this code from a tutorial I found on the internet, but I do not understand why I have to register my class for JSONRequestOperation
if I want to get JSON Data back? What exactly happens under the hood if I am doing so? What does the registerHTTPOperation
class exactly do?
PS: Is there a good documentation with examples and in-depth explanations of AFNetworking
somewhere on the internet?
The registerHTTPOperationClass:
on AFHTTPclient
will use the class set to handle you request when using the getPath and postPath methods.
In your given example, the HTTPRequestOperation is set the the AFJSONRequestOperation
, which means that all request to the server will be done with an instance of AFJSONRequestOperation
. The AFJSONRequestOperation
will try and parse the result from the server with the NSJSONSerialization
class.
If you server responds with XML you should set the HTTPRequestOperation to AFXMLRequestOperation
for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With