July 24, 2013

Major upgrades to Xamarin’s platform: Async is here

By

Today we are happy to introduce a major upgrade to the Xamarin platform. We are bringing C# 5.0 with async to all of our supported platforms. Async greatly simplifies the creation of responsive user experiences, which are particularly important for mobile applications.

We use apps to make the spare moments in life productive, and to help us better navigate the world with real-time, contextual information. Fast, fluid, instant-gratification apps are a must for mobile applications and C# async gives developers the tools to make this happen.

Until today, the only option for you to build responsive apps that work around network delays, connectivity issues, and complex data queries and processing is to use traditional asynchronous programming techniques. While leveraging common asynchronous patterns would lead to a better app for your users, they are not easy on the developer. You can quickly end up in a situation where your code has a cascade of nested callbacks that make it difficult to debug problems and create more opportunities for error. Even worse, sometimes the complexity is such that developers tend to cut corners on some code paths, leading to suboptimal experiences when applications are used in the wild.

The async and await keywords in C# 5.0 now available to Xamarin developers make asynchronous programming incredibly pleasant. You end up with code that is much more linear and much easier to understand. The compiler does a lot of magic for you which simplifies your code and your life. The following comparison demonstrates the impact of using C# async in your apps. The first version is implemented without async:

private void SnapAndPost ()
{
	Busy = true;
	UpdateUIStatus ("Taking a picture");
	var picker = new Xamarin.Media.MediaPicker ();
	var picTask = picker.TakePhotoAsync (new Xamarin.Media.StoreCameraMediaOptions ());
	picTask.ContinueWith ((picRetTask) => {
		InvokeOnMainThread (() => {
			if (picRetTask.IsCanceled) {
				Busy = false;
				UpdateUIStatus ("Canceled");
			} else {
				var tagsCtrl = new GetTagsUIViewController (picRetTask.Result.GetStream ());
				PresentViewController (tagsCtrl, true, () => {
					UpdateUIStatus ("Submitting picture to server");
					var uploadTask = new Task (() => {
						return PostPicToService (picRetTask.Result.GetStream (), tagsCtrl.Tags);
					});
					uploadTask.ContinueWith ((uploadRetTask) => {
						InvokeOnMainThread (() => {
							Busy = false;
							UpdateUIStatus (uploadRetTask.Result.Failed ? "Canceled" : "Success");
						});
					});
					uploadTask.Start ();
				});
			}
		});
	});
}

And this version was implemented with the benefit of async:

private async Task SnapAndPostAsync ()
{
	try {
		Busy = true;
		UpdateUIStatus ("Taking a picture");
		var picker = new Xamarin.Media.MediaPicker ();
		var mFile = await picker.TakePhotoAsync (new Xamarin.Media.StoreCameraMediaOptions ());
		var tagsCtrl = new GetTagsUIViewController (mFile.GetStream ());
		// Call new iOS await API
		await PresentViewControllerAsync (tagsCtrl, true);
		UpdateUIStatus ("Submitting picture to server");
		await PostPicToServiceAsync (mFile.GetStream (), tagsCtrl.Tags);
		UpdateUIStatus ("Success");
	} catch (OperationCanceledException) {
		UpdateUIStatus ("Canceled");
	} finally {
		Busy = false;
	}
}

With this release, we’ve gone beyond just adding async support to our base class libraries. We’ve actually asyncified native iOS and Android APIs, making it possible to take advantage of async in the platform-specific areas of your apps—something no other programming language can do, and further cementing C# as the best language for mobile development.

In addition to C# 5.0 async, this Xamarin update also upgrades Xamarin’s Mono to 3.0 for Xamarin.Android, Xamarin.Mac and Xamarin.iOS. Developers will benefit from over 7000 individual commits made to Mono during the past two years. These improvements include new .Net 4.5 APIs, async-friendly System.Net.Http, better debugging support, a faster version of our garbage collector and we eliminated the frustration of dealing with generics on iOS devices. We’ve also made it much faster for you to build and deploy Xamarin.iOS apps to devices for testing (up to seven times faster!), and we’ve made it easier to create bindings of Android native jar files. See the release notes for Xamarin.iOS and Xamarin.Android for all of the details.

I’ll be hosting a live webinar on August 15th to talk about how and why you should be using async in your mobile apps. Please join me!


Current subscribers can fire up Xamarin Studio or Visual Studio to update the latest version.  If you’re not a current subscriber, visit store.xamarin.com or download our free Starter Edition.

TwitterFacebookGoogle+LinkedInEmail