C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin


Published on

Fast, fluid, instant-gratification apps are a must for mobile and C# async makes this much easier to accomplish.

Slides are from the Xamarin CTO and cofounder Miguel de Icaza to understand how and why you should use async for your mobile apps.

Published in: Technology

C# Async on iOS and Android - Miguel de Icaza, CTO of Xamarin

  1. 1. C# Async on iOS and Android Miguel de Icaza, CTO @migueldeicaza
  2. 2. C# 5.0 Async on iOS and Android • Just released our C# 5 compiler – Async programming • Support both .NET Base Class Libraries Async • Async-ready APIs for iOS and Android
  3. 3. Asynchronous Programming Desire to build responsive applications
  4. 4. Fluid Uis allows for 60 fps 16m 32ms 64ms
  5. 5. Respond to user input
  6. 6. Responding to Input Get events Dispatch Update display
  7. 7. Get events Dispatch Update display
  8. 8. C# and Lambdas • They add a whole level of sanity • Shared state across lambdas – Compiler does the heavy lifting • Requires all lambdas to share a scope
  9. 9. Events: Some idioms
  10. 10. Perform Long-Running Operations
  11. 11. Long-running tasks • Background threads • Setup your own handshake protocol • Various idioms available
  12. 12. Slow file system or database access
  13. 13. Network access
  14. 14. More callbacks Get events Dispatch Update display
  15. 15. Get events Dispatch Update display DB Net
  16. 16. Fluid Uis allows for 60 fps 16m 32ms 64ms
  17. 17. Callback Hell
  18. 18. Just the other day
  19. 19. Callback Hell
  20. 20. Problems with callbacks • Control flow is difficult – Achieved by choosing what to call next – Tracking state across callbacks • Updating code is cumbersome – Error prone • Error reporting is bolted on the basic framework – Poor, and recovery is typically obnoxious, or non-existent
  21. 21. Go To Statement Considered Harmful “our intellectual powers are rather geared to master static relations and that our powers to visualize processes evolving in time are relatively poorly developed. For that reason we should do our utmost to shorten the conceptual gap between the static program and the dynamic process, to make the correspondence between the program (spread out in text space) and the process (spread out in time) as trivial as possible.” – Edsger W Dijkstra
  22. 22. How did we cope with GoTo? • New idioms – Structured Programming • Better languages – We had the compiler do the work for us
  23. 23. Callback Hell
  24. 24. C# Async – The Calm after the Storm
  25. 25. Evolution: Tasks • Reasoning about background operations – Task and Task<T> types • They encapsulate a unit of work, and have: – State: running, finished, cancelled – Result – Exceptions thrown (if any)
  26. 26. Thinking with Tasks • Many useful operations: – Wait: Task.WaitAny (t1, t2, t3) – Wait: Task.WaitAll (t1, t2, t3) – Chain: task1.ContinueWith (task2); – Wrap: Task.WhenAny (t1, t2, t3); – Wrap: Task.WhenAll (t1, t2, t3);
  27. 27. Writing beautiful code
  28. 28. ShowAlert – creating your own
  29. 29. With UIKit Animation
  30. 30. Async Support • Base Class Libraries: – Expect all the standard Async APIs from .NET BCL – Coverage is .NET 4.5 Complete for BCL • More Async Sources: – Xamarin.iOS: 174 – Xamarin.Android: 337 – Xamarin.Mobile – Xamarin Component Store
  31. 31. What is available as Async? • Simple Rule: – Any API that would take more than 50ms to run