NSURLConnection or NSObject which implements NSURLConnection - Subclass Concept with Objective-C

151 views Asked by At

Just thinking and trying to understand how I should implement a NSURLConnection in Objective-C. By the way; i'd like to connect different servers for some logging / download jobs ( ! multiple connections ).

I added a new Objective-C class, based on NSObject. Then I added my properties NSData, NSURLConnection and the needed delegates. The stuff works quite fine, connection, upload also the download.

For every single connection I have to copy the class or implement some logic into one class to distinguish the multiple connections.

Which is the prefered way for, generally, subclass NSURLConnection? With C++ I would create the subclass of NSURLConnection, add my properties and overload construction / destruction?

Conceptional question :-(

Thanks!

1

There are 1 answers

0
Jon Shier On

Generally a vending paradigm is used, where some class takes in an endpoint URL and generates the NSURLRequest and NSURLConnection to be used. In your instance, the class you currently have could serve as a good abstraction, where the managing class would create an instance with a particular URL and the class you've created would handle the specific delegate responses.

Another options, if you aren't opposed to using dependencies, is using AFNetworking, which has functionality to vend you NSURLConnections wrapped in NSOperations (or NSURLSessionTasks). You can look at AFNetworking's AFHTTPRequestOperationManager class if you'd like some inspiration. Essentially, the manager vends underlying AFHTTPRequestOperations, which are NSOperation subclasses that adopt the NSURLConnection delegate methods to asynchronously handle the connections.