c# async on ios and android - miguel de icaza, cto of xamarin

31
C# Async on iOS and Android Miguel de Icaza, CTO @migueldeicaza

Upload: xamarin

Post on 10-May-2015

4.242 views

Category:

Technology


1 download

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

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

C# Async on iOS and Android

Miguel de Icaza, CTO@migueldeicaza

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

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

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

Asynchronous Programming

Desire to build responsive applications

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

Fluid Uis allows for 60 fps

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

Respond to user input

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

Responding to Input

Get events

Dispatch

Update display

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

Get events

Dispatch

Update display

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

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

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

Events: Some idioms

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

Perform Long-Running Operations

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

Long-running tasks• Background threads

• Setup your own handshake protocol

• Various idioms available

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

Slow file system or database access

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

Network access

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

More callbacks

Get events

Dispatch

Update display

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

Get events

Dispatch

Update display

DB

Net

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

Fluid Uis allows for 60 fps

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

Callback Hell

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

Just the other day

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

Callback Hell

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

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

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

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

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

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

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

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

Callback Hell

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

C# Async – The Calm after the Storm

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

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)

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

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

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

Writing beautiful code

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

ShowAlert – creating your own

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

With UIKit Animation

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

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

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

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