Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Conquer the Network!

Conquer the Network!

This presentation is about how to build mobile apps that are connected to the network. I talk about how you can't rely on infinite and uninterrupted bandwidth and how you can leverage existing components from Nuget to make your app more resilient.

Roy Cornelissen

October 04, 2016
Tweet

More Decks by Roy Cornelissen

Other Decks in Technology

Transcript

  1. 8 fallacies of 1. The network is reliable. 2. Latency

    is zero. 3. Bandwidth is infinite. 4. The network is secure. 5. Topology doesn't change. 6. There is one administrator. 7. Transport cost is zero. 8. The network is homogeneous. Deutsch - 1994 Gosling - 1997 distributed computing
  2. Calling .Run(), .Wait() or .Result on a Task async Task<bool>

    MyMethod(int parameter) { } MyMethod(10).Run(); This will execute the task on the same thread, blocking the UI thread var result = await MyMethod(10); // async all the way! Pitfall
  3. Unnecessarily returning to the UI Thread await DoLotsOfNetworkCalls(); public async

    Task DoLotsOfNetworkCalls() { await httpClient.GetAsync(“…”); // do some calculations or de-serialization await httpClient.GetAsync(“…”); } After each await, execution will continue on the original thread, causing interruptions and even possibly deadlocks. await httpClient.GetAsync(“…”).ConfigureAwait(false); // use ConfigureAwait(false) in your library code Pitfall
  4. Creating async void methods async void MyMethod(int parameter) { }

    Awaiting this method will fail This is a Fire & Forget call, you cannot do any proper error handling async Task<bool> MyMethod(int parameter) { } async void OnClick(object sender, EventArgs e) { } // Exception: async event handlers! Pitfall
  5. Goals • Easily access RESTful API’s • Fast response to

    the user • Work offline • Deal with errors
  6. Easily access Restful Api’s Refit • Install-Package Refit Turns a

    RESTful API into a typesafe interface public interface IGitHubApi { [Get("/users/{user}")] Task<User> GetUser(string user); } Component by: Paul Betts, http://github.com/paulcbetts/Refit
  7. Faster response Akavache • Install-Package Akavache On device cache based

    on SQLite local database. You must include SQLite in the Windows app as well. Component by: Paul Betts, http://github.com/paulcbetts/Akavache
  8. SQLite.org SQLite • Cross-platform format; stored as a single file

    • 32 or 64 bit; big or little endian • Most of SQL92 • Missing some OUTER joins • Only supports TABLE RENAME & ADDCOLUMN • Views are read only
  9. Configuration – SQLite just needs to know where the database

    file is stored. – The location must be writable! • If you bundle a database file in your app, copy it out first! • Different platforms “care” about different things • Android: choose internal or external storage • iOS: choose Documents, Library or other
  10. public class Session { [PrimaryKey, AutoIncrement, Column("_id")] public int Id

    { get; set; } public string SpeakerName { get; set; } public string Title { get; set; } public string Abstract { get; set; } public string Location { get; set; } public DateTime Begins { get; set; } public DateTime Ends { get; set; } } Model Classes
  11. • The SQLite library is not thread safe • Don’t

    use the same Connection across threads • Always use lock () statements lock (locker){ // Do your query or insert here } Threading
  12. Core OS layer OS API layer Mono stack Application layer

    ModernHttpClient Faster network requests
  13. ModernHttpClient • Install-Package ModernHttpClient Leverages the native OS network stack

    by swapping in a NativeMessageHandler: • NSURLSession on iOS • OkHttp on Android Beware: exceptions are not the same as with plain HttpClient (e.g. Java web exceptions) Beware: iOS App Transport Security (use TLS2.0 edit info.plist) Component by: Paul Betts, http://github.com/paulcbetts/ModernHttpClient Faster network requests
  14. Use AndroidClientHandler Uses native java.net.URLConnection Faster network requests (alternative) var

    client = new HttpClient ( new Xamarin.Android.Net.AndroidClientHandler ());
  15. Connectivity Plugin • Install-Package Xam.Plugin.Connectivity Provides a cross platform way

    to check the connectivity of the device before trying to issue a network request. Component by: James Montemagno, https://github.com/jamesmontemagno/Xamarin.Plugins Work Offline
  16. What do you do? Retry • How many times? •

    At what interval? • What if the server is already busy? Circuit breaker • Elegant pattern for failing fast after network errors
  17. Polly • Install-Package Polly Implement error handling policies by declaratively

    specifying retry, circuit breaker etc. Polly will do the heavy lifting. Polly by: Michael Wolfenden, https://github.com/michael-wolfenden/ Handle errors
  18. Fody.AsyncErrorHandler • Install-Package Fody.AsyncErrorHandler Globally handle errors that happen in

    async (background) code. Useful for catching otherwise hidden errors. Uses a Fody IL weaver. Fody.AsyncErrorHandler by: Simon Cropp, https://github.com/Fody/AsyncErrorHandler Handle errors
  19. Easily access RESTful API’s: Refit Fast response to user: Caching,

    ModernHttpClient, async Work offline: Akavache, Connectivity Plugin Deal with errors: Polly, Fody.AsyncErrorHandler Goals