C# Async on iOS and Android - Craig Dunn, Developer Evangelist at Xamarin

7,516 views
7,121 views

Published on

Fast, fluid, instant-gratification apps are a must for mobile and C# async makes this much easier to accomplish. Xamarin Developer Evangelist, Craig Dunn, demos C# async on mobile apps.

Slides are from the Xamarin webinar on C# Async on iOS and Android on August 15, 2013.

Published in: Technology, Art & Photos
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,516
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
115
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • Async IO http://msdn.microsoft.com/en-us/library/kztecsys.aspx
  • Await, and UI, and deadlocks! http://blogs.msdn.com/b/pfxteam/archive/2011/01/13/10115163.aspx
  • SIMPLE APP : download string and image example
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • Solution & Projects can be shared across development operating systems Both IDEs have familiar UI > solution, properties, editor, debugger
  • C# Async on iOS and Android - Craig Dunn, Developer Evangelist at Xamarin

    1. 1. C# Async on iOS and Android Craig Dunn Developer Evangelist at Xamarin @conceptdev
    2. 2. .NET BCL APIs • Lots of .NET APIs support async; for example: HttpClient.GetStringAsync() HttpClient.PostAsync() FileStream.ReadAsync() FileStream.CopyToAsync() • and many more... Complete .NET 4.5 coverage for BCL
    3. 3. iOS • updated iOS APIs to be async; some examples: ALAssetsLibrary.WriteImageToSavedPhotosAlbumAsync () ALAsset.SetImageDataAsync() SKStoreProductViewController.LoadProductAsync() CLGeocoder.GeocodeAddress() NSUrlConnection.SendRequestAsync() and many more... 174 async iOS native APIs
    4. 4. Android • updated Android APIs to be async; some examples: Android.Net.Http.AndroidHttpClient.ExecuteAsync() Android.Bluetooth.BluetoothServerSocket.AcceptAsync( ) Android.Graphics.BitmapFactory.DecodeFileAsync() Android.Locations.Geocoder.GetFromLocationAsync() Java.IO.File.ListAsync() and many more... 337 async Android native APIs
    5. 5. Xamarin APIs • Xamarin.Mobile Geolocator.GetPositionAsync() Contact.SaveThumbnailAsync() • Xamarin.Auth FormAuthenticator.SignInAsync() Request.GetResponseAsync(cancellationToken) • and many more... Xamarin cross-platform libraries
    6. 6. Xamarin Component Store • Many components already offer async APIs, eg. Parse! Powerful, easy to use components http://components.xamarin.com
    7. 7. Using Async async, await, cancellation and error handling
    8. 8. DEMO1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 4) Return Html length
    9. 9. Comparison 1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 5) Error Handling 4) Return Html length 6) InvokeOnMainThread Callback Hell
    10. 10. Comparison 1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 5) Error Handling 4) Return Html length 6) InvokeOnMainThread Callback Hell
    11. 11. 1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 5) Error Handling 4) Return Html length Comparison Async-ified
    12. 12. 1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 5) Error Handling 4) Return Html length Comparison 6) InvokeOnMainThread one place not required Async-ified
    13. 13. Comparison 1) Download Html string 2) Download Jpeg image 3) Save to Photo Album 5) Error Handling 4) Return Html length 6) InvokeOnMainThread old new!
    14. 14. Compiler Magic • async keyword informs the compiler that this method needs to be “munged” (my term) • await keyword indicates a suspension point where a callback needs to be generated, along with error handling • Continuations are generated after each suspension point • Error handling (support for enclosing try-catch) is taken care of • All you need to remember is async and await • and use Tasks
    15. 15. How to use: async? • async modifier on methods, lambdas and anonymous methods use Async suffix, eg LoadAsync, SendAsync return Task or Task<T> preferably- Task for methods that don’t return a value - Task<T> to return a value - void for event handlers only void for event handlers
    16. 16. How to use: await? • await keyword on awaitable objects (Tasks) only within an async context marks a suspension point - control is returned to caller can’t be used in catch or finally blocks Task, Task<T> or a custom type get a reference to the Task first ... or just await
    17. 17. How to use: error handling? • async Task or Task<T> Returned Task State == Faulted Exception re-thrown when task is awaited • async void methods (event handlers) Exception is thrown on the current synchronization context and the app will crash...
    18. 18. How to use: cancellation?
    19. 19. How to use: progress reporting? send events to track progress
    20. 20. BONUS: Combinators • Wait on multiple tasks Task.WhenAll (IEnumerable<Task>) - requires all the tasks to be completed Task.WhenAny(IEnumerable<Task>) - returns when any of the tasks completes this kind of loop fine for small numbers of Tasks
    21. 21. Don’t await... give it a try • Docs, Recipes docs.xamarin.com/ • Forums forums.xamarin.com • Samples github.com/xamarin xamarin.com/download
    22. 22. Questions?

    ×