Implementing ModernHttpClient in MvvmCross

Earlier this week Paul Betts started a new open source project called ModernHttpClient which is a Xamarin wrapper around two native libs.

“This library brings the latest platform-specific networking libraries to Xamarin applications via a custom HttpClient handler. Write your app using System.Net.Http, but drop this library in and it will go drastically faster. This is made possible by two native libraries:
On iOS, AFNetworking 1.3.3
On Android, via OkHttp 1.2.1″

While Paul does claim your app will go drastically faster he’s not actually very specific about why it will go drastically faster but after a query I’m now aware that for iOS the AFNetworking library has better thread handling than the standard httpclient. So after some tests I confirmed that for single requests the webclient is no faster than the standard client, I’m yet to test performance in an app with many simultaneous client requests but in theory it should be faster.

If you’re using the Image Downloader in MvvmCross implementing the library is a little tricky, but luckily Stuart has done a great job of keeping it extensible.

To get started you’ll need to clone the repo but because it has external projects you’ll need to use the recursive command.

UPDATE, there’s an easier way to get it: Paul Betts Comment

git clone –recursive

Once you’ve got a copy of it you’ll need to build both the Xamarin binding for AFNetworking and the ModernHttpClient.

The binding solution is in vendor/AFNetworking and the ModernHttpClient is in the root directory.

Now that you’ve built both the assemblies you need to add them to your Xamarin.iOS solution.

After a little dive into the MvvmCross source code I discovered that there’s a simple interface that I can implement and register. If you’re not sure what I’m talking about you can read up on MvvmCross plugins here:

So in the Xamarin.iOS project create a plugins directory and add two files MvxFastHttpFileDownloader and MvxFastFileDownloadRequest.

Because the standard MvvmCross plugins have already registered the interface IMvxhttpFileDownloader you need to make sure you register your downloader last, so put it at the bottom of the InitializeLastChance overide in your setup.cs file.





4 Responses

Leave a Reply