Do you ever wonder if the code you are writing is good? If it is the best you can do? How do you know if the classes you are building are extensible? If the architecture you created for your app will actually make it’s maintenance less scary? Well, I believe that these thoughts never go away, it is an intrinsic part of being a developer. To question yourself is to always know that there is room for learning and to improve your code!
With these thoughts in mind I decided to share a few tips and tricks on how to architect your app’s http services. Things like data caching, http requests retry policy, off-line sync, local vs remote requests all of the architecture to consume Rest apis from your app!
1. Http Service Architecture
This is the base and most important part of our work, we will give some special attention to it. As always, all the code that we will be using here is available on GitHub check it out. Parts of this sample are brought to you by Hotel 360 app, I have been checking out this code that Microsoft built and I strongly suggest everyone to check it out, there are a lot of awesome examples there!
Let’s draw the idea of the solution presented in the GitHub sample app code:
We have this as a generic service that will have all the common methods that are used on API requests like Get, Post and Put; also other helper methods to setup the request headers and to handle responses from the APIs. The code on this class was put here to avoid code repetition. The way RequestService is built makes it completely extensible, if we need any new services from the apis that we consume, we will just have to create a new specific service and inject RequestService into it.
This is the specific request service where we are going to inject the generic service (RequestsService). In this class we decide which api methods we will use and do some other things like building the url.
Well, view models are the core of our MVVM approach, if you need more information on them check this link out, we are basically going to inject on the view models the specific request service, like PersonService, MonkeyService or any other services we need to use on our view models.
2. HttpClient as Singleton
Long story short, every time you create a instance of the HttpClient class you are also creating connections that will be held in TIME_WAIT state for up to 240 seconds, this can cause a socket exhaustion between your app and your API. Please check out this blog post that will give you all of the detailed information you need to know!
3. Native HttpClient Implementation
Let the target platforms handle the HttpClient implementation, instead of the mono stack. It is much faster and very simple to set up.
VS for mac: iOS project Options -> iOS Build -> HttpClient implementation -> NSUrlSession (iOS7+) VS for windows: iOS project properties -> iOS Build -> HttpClient implementation -> NSUrlSession (iOS7+)
Check out this link to learn more about it in the iOS target platform.
VS for mac: Android project Options -> Android Build -> HttpClient implementation -> NSUrlSession (iOS7+) VS for windows: Android project properties -> Android Options -> Advanced -> HttpClient implementation -> AndroidClientHandler (iOS7+)
Check out this link o learn more about it in the Android target platform.
If you don’t know about this yet, you should definitely check it out here, you don’t want your app crashing due do deadlocks =O
- Make sure all your API urls are using HTTPS or else the HttpClient Implementation on step 3 will throw exceptions on you.
- This is a personal tip from Causer, Keep an eye on the community, keep an eye on how people code and do it your self (practice). These are the best ways, in my opinion, to improve your coding skills and become a better developer.