Reputation: 564
So, I'm using AFNetworking for an iOS app and I'm just going through a bit of refactoring to include some network reachability code. My previous design pattern for marshaling parameters for requests and the like has been to create create service specific request parameters and then use those to create an NSURL and, subsequently, an NSURLRequest.
The services I'm connecting to are all over the place — some require signed requests, access tokens, muddled bitters, etc. (Twitter, Foursquare, Instagram, Tumblr, etc.) The usual.
For each service, based on a general, service-agnostic protocol I have methods that are able to retrieve status, retrieve profile data for users, etc. Within these methods, I create an AFHTTPClient and that works fine.
But, now that I'm adding a some application semantics [AFHTTPClient setReachabilityStatusChangeBlock:] I wonder if I should be creating an AFHTTPClient for each instance of the class that handles a variety of service requests, rather than one for each method within that class that handles service requests? Having the reachability status change block defined
Does anyone have suggestions as to a good design pattern to use in this case? I mean — I could sort something out, but I'm wondering if there's a "best practices" / "built from experience" pattern?
Generally, my classes that handle requests from specific services are long-lived..there's one for every "user" logged in during runtime, although the number of users is typically quite small – in the dozens.
I guess what I'm asking is — generally, where do people generally define their AFHTTPClient, and what generally do they have the network reachability status change block do? (Instinct says: cancel operations, alert the user if the status indicates that the internet is gone, certainly — but any clever "retry" semantics, etc?)
Upvotes: 1
Views: 713
Reputation: 76
In my applications is stick to rule "one APIClient per API Server". This is convenient, when adding different bits and tricks to each APIClient. They could differ by authorisation type and encryptions.
Regarding Reachability. Did you tried using NSNotificationCenter
for that purpose. That's what is was created for - to populate widely used events. You can check Reachability library and it's examples.
Upvotes: 1