2

I have subclassed AFHTTPSessionManager and added some methods specific to my implementation. I have a couple questions around using my subclass as a singleton and concurrency:

  • Is creating a singleton of AFHTTPSessionManager still a best practice? (AFNetworking 2.0, iOS7)

  • If I make requests via a singleton of the subclass using the [self GET/POST method does this support concurrent operations? E.g., I have a slow sync running and then do an search. Will the search begin immediately or wait for the sync to complete? Or, asked another way, are these operations on independent operation queues?

alexphilipp
  • 215
  • 2
  • 9

1 Answers1

6

You asked:

Is creating a singleton of AFHTTPSessionManager still a best practice? (AFNetworking 2.0, iOS7)

I'm not sure if it ever was best practice. Singletons are frequently derided (see What's wrong with singleton?, which has links to lots of arguments against singletons). They are convenient, but for most apps, a singleton for the session manager is unnecessary. My general counsel is that you shouldn't use a singleton unless you have some compelling need to do so.

This is a subject of opinion and debate (which is explicitly frowned upon on Stack Overflow), so I would not suggest we pursue that question further. Follow the links in the above Stack Overflow question, and you'll see many opinions expressed there.

If I make requests ... does this support concurrent operations?

Yes, the network requests run asynchronously and support concurrent operations.

Are these operations on independent operation queues?

At the time I write this, the requests generated via AFHTTPSessionManager are not run on operation queues at all. The session's manager NSURLSession manages the tasks itself.

On the other hand, the NSURLConnection-based AFHTTPRequestOperationManager will run GET and POST requests on a single, concurrent operation queue. If you manually create your own AFHTTPRequestOperation, you can add them to your own queues if you want.

But all of this is academic. I think your real question is whether GET and POST requests run asynchronously, and the answer is "yes". And if the question is whether they run concurrently with respect to each other, again, the answer is "yes".

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 2
    Great, thank you. That explains the downvotes. In this specific case, the singleton pattern is recommended by the author in the docs: http://cocoadocs.org/docsets/AFNetworking/2.0.0/Classes/AFHTTPSessionManager.html – alexphilipp May 23 '14 at 21:56
  • ***"My general counsel is that you shouldn't use a singleton unless you have some compelling need to do so."*** A singleton AFHTTPSessionManager object will retain the request serializer and response serializer configuration, they won't be initialized for later requests. I didn't compute the memory usage occupations, yet, they're inherited from NSObject and there are few heavy properties. Does this count for? – Itachi May 24 '17 at 03:24
  • That's an argument for not re-instantiating the session manager all the time, but that's not the same as singleton. There are other patterns (e.g., dependency injection) that don't involve reinstantiating, but don't entail the problems of singletons. Having said that, using singletons for network manager isn't the worst pattern I've seen (and, in fact, I've even done it). My point is merely that many gravitate too quickly to singletons without thought to the implications and alternatives. But I don't think we should re-litigate singletons here. It's been covered exhaustively elsewhere. – Rob May 24 '17 at 06:29