February 26, 2011

Async Web Service Timout

When using SOAP-based web services (in this case with MonoDevelop / MonoTouch) I found I had to rely on the asynchronous proxy web methods to keep my UI responsive. Pretty common, right? Unfortunately, the asynchronous web methods seem to ignore the TimeOut property on the service. Without a timeout my users could be left waiting indefinitely on an unresponsive server. Not acceptable.

To get around this issue I came up with a way to cancel an asynchronous web method request after a set period of time using a Timer object. Maybe it can help others in a similar predicament? Here's some sample code:

protected void GetServiceData()
{
//Indicates that network activity is going on
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = true;

//Make the async call
using (MyService service = new MyService())
{
//Timer is set to go off one time after 15 seconds
Timer serviceTimer = new Timer(15000);
serviceTimer.AutoReset = false;
serviceTimer.Elapsed += delegate(object source, ElapsedEventArgs e) {
service.Abort();
throw new WebException("Timeout expired!");
};
serviceTimer.Enabled = true;

//Call the desired web method
service.WebMethodCompleted += ServiceWebMethodCompleted;
service.WebMethodAsync(serviceTimer);
}
}

//The async callback method
protected void ServiceWebMethodCompleted(object sender, WebMethodCompletedEventArgs e)
{
using (NSAutoreleasePool pool = new NSAutoreleasePool())
{
//Disable the timer that would abort this call with an exception
//if the call to this web method took too long
Timer serviceTimer = e.UserState as Timer;
if (serviceTimer != null)
{
serviceTimer.Enabled = false;
serviceTimer.Dispose();
}

if (e.Error != null)
{
if (e.Error is WebException)
{
//An error due to a timeout happened - handle it here
}
else
{
//Handle all other errors here
}
}
else
{
//Async call successful - do something cool with e.Result
}

//Indicates network activity has finished
this.InvokeOnMainThread(delegate() {
UIApplication.SharedApplication.NetworkActivityIndicatorVisible = false;
});
}
}

September 18, 2010

Geo Rally for the iPhone

Geo RallyMy side project, Durbinware, released our first iPhone app, Geo Rally, to Apple's App Store back in May. It allows users to create, share and challenge timed point-to-point treasure hunts. If you've seen the movies Rat Race, or Midnight Madness you'll understand the basic concept. The first point, or starting line, is revealed at the beginning of the challenge and then each consecutive waypoint requires users to solve text-based clues or riddles to proceed. Solving a clue and arriving at the next location triggers another clue and so on until the user arrives at the last point, or finish line. Different users can compete against the same course, or Rally, and the app will optionally tweet race completion times for bragging rights. It has the potential to facilitate a great team building experience among youth groups, friends or co-workers. If this MonoTouch based app sounds interesting to you then by all means please download it.

I had a great time working on Geo Rally and was very thankful for MonoTouch. Coming from a .NET background I was a little overwhelmed at the prospect of learning a new language (Objective C) and iOS's multitude of supporting core APIs. MonoTouch eliminated one of these roadblocks so I could hit the ground running in a familiar language and branch out into unknown APIs as needed. In the end I believe this made me more productive. And now, coupled with MonoDroid and Windows Phone 7, there are even more options for code reuse (everything but the UI). It's a good time to be a C# developer!

July 10, 2010

iOS 4 and Map Kit Overlays with MonoTouch

One of the "over 100 new features" in iOS 4 that I've been especially interested in are Map Kit overlays. Previously, developers were limited to using annotations to express points on a map. If you wanted to draw a shape of some sort you were more or less up a creek.

Overlays are a special kind of annotation designed to represent an area on a map. iOS 4's Map Kit comes with some common shapes built in (rectangles, circles, polygons, etc). As I understand it it's also possible to make your own custom shapes.

Each overlay object holds data to represent the shape and has a corresponding view that tells the MKMapView's delegate how to draw the overlay. As an example here's how one might draw a circle overlay with a 100 meter radius around the Empire State Building. The code below shows how to do this in C# via MonoTouch.

CLLocationCoordinate2D empireStBld = new CLLocationCoordinate2D(40.748433, -73.985656);
double radiusInMeters = 100d;
MKCircle circle = MKCircle.Circle(empireStBld, radiusInMeters);
MapView.Delegate = new MapViewDelegate(circle);
MapView.AddOverlay(circle);


The only thing left is that the MapViewDelegate class needs to be set up to give an appropriate view for the overlay:

public class MapViewDelegate : MKMapViewDelegate
{
private MKCircle _circle = null;
private MKCircleView _circleView = null;

public MapViewDelegate(MKCircle circle)
{
_circle = circle;
}

public override MKOverlayView GetViewForOverlay(MKMapView mapView, NSObject overlay)
{
if ((_circle != null) && (_circleView == null))
{
_circleView = new MKCircleView(_circle);
_circleView.FillColor = UIColor.Cyan;
}
return _circleView;
}
}