Using the NSURLConnection's own subclass, how does it “find” extra data in the class later?

This blog offers a good solution for handling multiple NSURLConnections: create a custom class called "CustomURLConnection" that has an additional tag property.

http://blog.emmerinc.be/index.php/2009/03/02/custom-nsurlconnection-class-with-tag/

http://blog.emmerinc.be/index.php/2009/03/15/multiple-async-nsurlconnections-example/

Basically, he just added the tag property to the existing NSURLConnection:

CustomURLConnection.m

 - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately tag:(NSString*)tag { self = [super initWithRequest:request delegate:delegate startImmediately:startImmediately]; if (self) { self.tag = tag; } return self; } 

then, in the normal NSURLConnection boot mode, you can do:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection { //Log the connection's tag CustomURLConnection *ttttag = (CustomURLConnection *)connection; // **HERE** NSLog(@"%@", ttttag.tag); NSMutableData *dataForConnection = [self dataForConnection:(CustomURLConnection*)connection]; [connection release]; } 

So, I have a problem. The way I see it, this is how things are:

  • I create a "connection tag +"
  • The first code snippet I posted above creates a regular “no tag ”, which ultimately will call the usual NSURLConnection methods, such as connectionDidFinishLoading . What happens to tag at this moment?
  • In the connectionDidFinishLoading method, I can return the connection back to the "connection +" tag, and then find the information about the lost tags that was dropped. How?

Maybe I'm just confusing, but it seems that the tag was dropped when it starts the normal NSURLConnection path. But then, dropping it as a subclass, I can again restore the tag property. Where did he live / go on average?

Can someone with a better understanding of inheritance explain this to me?

+6
source share
2 answers

With this code:

 [[CustomURLConnection alloc] initWithRequest:... delegate:... startImmediately:... startImmediately tag:...]; 

an instance of CustomURLConnection is created. Now here is where your understanding is wrong: this CustomURLConnection object can freely call all methods of its superclasses, but it will always remain CustomURLConnection . tag always present.

Methods defined in the superclass, such as initWithRequest:delegate:startImmediately: do not know about this tag, but they also do not need it. When the delegate method is called:

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

The connection argument is the same CustomURLConnection that you created above. The type in the method signature is different, but it does not matter; because you know that this connection is of type CustomURLConnection , you can simply give the connection object the correct type and access the new property. But even if you do not, the tag will still be there all the time.

+4
source

I'm not sure what you mean:

The first code snippet I posted above creates the usual “connection” (without a tag).

What you have done here, create a subclass of NSURLConnection . You can use the latter anywhere, you can use the first. NSURLConnection* means "pointer to NSURLConnection* or its subclass." So the original object you created was CustomURLConnection , and an additional ivar was added to it. This ivar does not disappear just because proxy users reference it with their superclass.

+1
source

Source: https://habr.com/ru/post/902773/


All Articles