reachability in mobile app development

23
The Importance of Reachability Marc Weil CTO, CloudMine [email protected]

Upload: marc-weil

Post on 06-May-2015

841 views

Category:

Technology


3 download

DESCRIPTION

A 10 minute talk I gave at the API Strategy Conference in NYC on the importance of using reachability detection in conjunction with message queues when communicating with web services from mobile devices. Includes specific examples for iOS.

TRANSCRIPT

Page 1: Reachability in Mobile App Development

The Importance ofReachability

Marc WeilCTO, CloudMine

[email protected]

Page 2: Reachability in Mobile App Development

Wireless connections aren’t perfect

Page 3: Reachability in Mobile App Development

Defensive Programming

Page 4: Reachability in Mobile App Development

Let’s look at a quick example

Page 5: Reachability in Mobile App Development

- (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; // responseBody contains the response body}

Why is this naive?

• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?

Page 6: Reachability in Mobile App Development

- (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }];}

Why is this still naive?

• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?

Page 7: Reachability in Mobile App Development

- (void)doLoad { NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; [NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *resp, NSData *body, NSError *err) { if (!err) { NSLog(@"Success!"); // body argument contains response body } else { NSLog(@"Error!"); } }];}

Why is this still naive?

• Potentially blocks UI thread• No error handling• What happens if there’s no network connection?

Page 8: Reachability in Mobile App Development

Queues are your best friend

Page 9: Reachability in Mobile App Development

Internet

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

When everything is dandy...

Request generator HTTP

Constantflow(th

is is

the q

ueue

)

Page 10: Reachability in Mobile App Development

Internet

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

When everything is not so dandy...

Request generator HTTP

Haltedflow

HTTP

HTTP

(this

is th

e que

ue)

Page 11: Reachability in Mobile App Development

How can I do this in code?

(apologies to Android devs)

Page 12: Reachability in Mobile App Development

Three components:

1. NSOperationQueue2. NSOperation3. Reachability

Page 13: Reachability in Mobile App Development

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; }];

Page 14: Reachability in Mobile App Development

NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [queue addOperationWithBlock:^{ NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"]]; NSHTTPURLResponse *response = [[NSHTTPURLResponse alloc] init]; NSData *responseBody = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:NULL]; }];

This is a convenience method that creates anNSBlockOperation under the hood.

Page 15: Reachability in Mobile App Development

Internet

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTPRequest

generator HTTP

Constantflow

(NSOperations)NSOperationQueue

NSOperation

Page 16: Reachability in Mobile App Development

What about Reachability?

Page 17: Reachability in Mobile App Development

Everything is contained inSystemConfiguration.framework

Page 18: Reachability in Mobile App Development

But don’t use that directly... it’s terrible

Page 19: Reachability in Mobile App Development

https://github.com/tonymillion/Reachability

(love abstraction!)

Page 20: Reachability in Mobile App Development

// allocate a reachability object Reachability* reach = [Reachability reachabilityWithHostname:@"www.google.com"]; reach.reachableOnWWAN = YES; // set the blocks reach.reachableBlock = ^(Reachability *reach) { queue.suspended = NO; }; reach.unreachableBlock = ^(Reachability *reach) { queue.suspended = YES; }; // start the notifier which will cause the reachability object to retain itself! [reach startNotifier];

(example from readme on GitHub)

Page 21: Reachability in Mobile App Development

https://github.com/AFNetworking/AFNetworking

Bringing out the big guns...(thanks Gowalla!!! <3)

POW!

Page 22: Reachability in Mobile App Development

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

One more pet peeve....are apps that don’t do this