reachability in mobile app development

Post on 06-May-2015

841 Views

Category:

Technology

3 Downloads

Preview:

Click to see full reader

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

The Importance ofReachability

Marc WeilCTO, CloudMine

marc@cloudmine.me

Wireless connections aren’t perfect

Defensive Programming

Let’s look at a quick example

- (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?

- (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?

- (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?

Queues are your best friend

Internet

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

When everything is dandy...

Request generator HTTP

Constantflow(th

is is

the q

ueue

)

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)

How can I do this in code?

(apologies to Android devs)

Three components:

1. NSOperationQueue2. NSOperation3. Reachability

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]; }];

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.

Internet

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTP

HTTPRequest

generator HTTP

Constantflow

(NSOperations)NSOperationQueue

NSOperation

What about Reachability?

Everything is contained inSystemConfiguration.framework

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

https://github.com/tonymillion/Reachability

(love abstraction!)

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

https://github.com/AFNetworking/AFNetworking

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

POW!

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

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

top related