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

Post on 10-May-2015

4.241 views 1 download

Tags:

description

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.

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

C# Async on iOS and Android

Miguel de Icaza, CTO@migueldeicaza

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

Asynchronous Programming

Desire to build responsive applications

Fluid Uis allows for 60 fps

Respond to user input

Responding to Input

Get events

Dispatch

Update display

Get events

Dispatch

Update display

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

Events: Some idioms

Perform Long-Running Operations

Long-running tasks• Background threads

• Setup your own handshake protocol

• Various idioms available

Slow file system or database access

Network access

More callbacks

Get events

Dispatch

Update display

Get events

Dispatch

Update display

DB

Net

Fluid Uis allows for 60 fps

Callback Hell

Just the other day

Callback Hell

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

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

How did we cope with GoTo?• New idioms– Structured Programming

• Better languages– We had the compiler do the work for us

Callback Hell

C# Async – The Calm after the Storm

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)

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);

Writing beautiful code

ShowAlert – creating your own

With UIKit Animation

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

What is available as Async?• Simple Rule:– Any API that would take more than 50ms to run