Online Book Reader

Home Category

iOS Recipes - Matt Drance [63]

By Root 206 0
if the count is positive, the network activity indicator is shown; when it goes back down to 0, the indicator is hidden. Because most of the UIKit is not understood to be thread-safe and the networkActivityIndicatorVisible property is not explicitly documented as such, we write a check to ensure the network activity indicator is touched only from the main thread.

NetworkActivityCenter/Classes/UIApplication+PRPNetworkActivity.m

- (void)prp_refreshNetworkActivityIndicator {

if (![NSThread isMainThread]) {

SEL sel_refresh = @selector(prp_refreshNetworkActivityIndicator);

[self performSelectorOnMainThread:sel_refresh

withObject:nil

waitUntilDone:NO];

return;

}

BOOL active = (self.prp_networkActivityCount > 0);

self.networkActivityIndicatorVisible = active;

}

We now have reliable network state management accessible from anywhere in our application and completely decoupled from the rest of our code. Just call ‑prp_pushNetworkActivity whenever starting a connection, and call ‑prp_popNetworkActivity whenever the connection terminates.

The NetworkActivityCenter sample project demonstrates this code in action. We’ve modified the PRPDownload class from an earlier recipe to push and pop activity based on the status of each download. Neither these download objects nor the test app’s view controller has any idea of one another, let alone what each is doing with the network. Each object reports its state to the UIApplication category methods, which decide when the network activity indicator should be activated or deactivated.

This project illustrates an application of the asynchronous PRPConnection mechanism from Recipe 32, Simplify Web Service Connections . We’ve tied a download to each row in the table and modified the PRPConnection class to use the category methods from this recipe. The network activity indicator shows as soon as downloads begin and automatically hides when the last download is finished or interrupted. The code you see in this class stays the same whether 1 or 100 downloads are in progress.

Recipe 32 Simplify Web Service Connections

Problem

You want to download data from a web service with minimal code and complexity. Where do you start?

Solution

The NSURLConnection class included with the iPhone SDK provides a clean, flexible interface for downloading web-based data of any type or size. It also requires a lot of repetitive setup code everywhere we intend to use it. Most of the time, we just want to kick off a download in the background and get our file or data back when it’s completed.

We can minimize the work necessary for each download and save ourselves a lot of repetition by wrapping a simpler interface around NSURLConnection. This class, PRPConnection, manages the temporary data structure and tracks progress that our own controller object would normally be responsible for so that the calling code needs to respond only when the download is complete and the data is ready. We’ll also create an optional hook for monitoring download progress.

The class contains an NSURLConnection object (of course), as well as numerous other pieces of information not provided by NSURLConnection:

Destination URL

Originating NSURLRequest

Expected download size

Download completion (percent) to date

SimpleDownload/Classes/PRPConnection.h

@property (nonatomic, copy, readonly) NSURL *url;

@property (nonatomic, copy, readonly) NSURLRequest *urlRequest;

@property (nonatomic, assign, readonly) NSInteger contentLength;

@property (nonatomic, retain, readonly) NSMutableData *downloadData;

@property (nonatomic, assign, readonly) float percentComplete;

@property (nonatomic, assign) NSUInteger progressThreshold;

We create the connection by simply passing the destination URL and optional Objective-C blocks for handling connection progress, completion, or failure. A convenience method for passing your own NSURLRequest is also there, which will come in handy once we discuss HTTP POST uploads in the next two chapters.

SimpleDownload/Classes/PRPConnection.h

+ (id)connectionWithURL:(NSURL

Return Main Page Previous Page Next Page

®Online Book Reader