Graphics Programs Reference
In-Depth Information
@synthesize request, completionBlock, xmlRootObject;
- (id)initWithRequest:(NSURLRequest *)req
{
self = [super init];
if (self) {
[self setRequest:req];
}
return self;
}
- (void)start
{
// Initialize container for data collected from NSURLConnection
container = [[NSMutableData alloc] init];
// Spawn connection
internalConnection = [[NSURLConnection alloc] initWithRequest:[self request]
delegate:self
startImmediately:YES];
}
Build the application and check for syntax errors.
There is one little hitch here. When the store creates an instance of BNRConnection
and tells it to start, the store will no longer keep a reference to it. This technique is known
as “fire and forget.” However, with ARC, we know that “forgetting” about an object will
destroy it. Thus, the BNRConnection needs to be owned by something. At the top of
BNRConnection.m , add a static NSMutableArray variable to hold a strong referen-
ce to all active BNRConnection s.
#import "BNRConnection.h"
static NSMutableArray *sharedConnectionList = nil;
@implementation BNRConnection
@synthesize request, completionBlock, xmlRootObject;
(As a bonus, you can use this connection list as a way to reference connections in pro-
gress. This is especially useful for cancelling a request.)
In BNRConnection.m , update start to add the connection to this array so that it
doesn't get destroyed when the store forgets about it.
- (void)start
{
container = [[NSMutableData alloc] init];
internalConnection = [[NSURLConnection alloc] initWithRequest:[self request]
delegate:self
startImmediately:YES];
// If this is the first connection started, create the array
if (!sharedConnectionList)
sharedConnectionList = [[NSMutableArray alloc] init];
// Add the connection to the array so it doesn't get destroyed
Search WWH ::




Custom Search