lørdag den 6. maj 2017

Artm Fetcher - A simple network layer library

Network layer in Xamarin apps


Introduction

Doing network such as downloading a JSON file from a webserver can be, perhaps surprisingly, difficult on mobile devices
  • You need to ensure you're using the platform-specific optimized network api calls to get SPDY, GZIP, HTTP/2, etc.
  • You need to have a retry mechanism if the server is unresponsive
  • You need to have a caching layer to
    • Minimize network usage, as connections are often metered
    • Improve performance
    • Handle unresponive servers / endpoints / no internet connection
  • You need to handle the Cold Start problem: the very first time an app is started and there is no valid internet available. You should be able to ship your app with preloaded data for a given url.

Example time!

Artm Fetcher solves this in a simple and easy to use manner. Use the following code in both your Android and iOS project:
IFetcherRepositoryStoragePathService path = new FetcherRepositoryStoragePathService();
IFetcherRepositoryService repository = new FetcherRepositoryService(path);
IFetcherWebService web = new FetcherWebService();

// Primary interface you should use
IFetcherService fetcher = new FetcherService(web, repository);

var url = new System.Uri("https://www.google.com");

// (Optional) Cold start: You can ship with preloaded data, and thus avoid
// an initial requirement for an active internet connection
fetcher.Preload(url, "<html>Hello world!</html>");

// Try our hardest to give you *some* response for a given url. 
// If an url has been recently created or updated we get the response from the local cache.
// If an url has NOT recently been created or updated we try to update 
// the response from the network. 
// If we cannot get the url from the network, and no cached data is available, we try to use preloaded data.
IUrlCacheInfo response = await fetcher.Fetch(url); 

Implementation details

Artm Fetcher uses SQLite for storing its cache. The file is called "fetcher.db3" by default and is stored in the apps internal storage.

Artm Fetcher implements an exponential backoff retry mechanism using Polly . Retries 5 times by default.

Get it here

NuGet: https://www.nuget.org/packages/artm.fetcher/

MvvmCross Plugin GitHub: https://github.com/mgj/MvvmCross-Plugins
MvvmCross Plugin NuGet: https://www.nuget.org/packages/artm.mvxplugins.fetcher/

Ingen kommentarer:

Send en kommentar