2
// AbstractNetworkOperation.m
5
// Created by Aaron Brethorst on 5/12/11.
6
// Copyright 2011 Canonical. All rights reserved.
9
#import "AbstractNetworkOperation.h"
11
#import "UONetworkStatusCoordinator.h"
14
@interface AbstractNetworkOperation ()
18
@implementation AbstractNetworkOperation
19
@synthesize url = _url;
20
@synthesize statusCode = _statusCode;
21
@synthesize error = _error;
22
@synthesize isExecuting = _isExecuting;
23
@synthesize isFinished = _isFinished;
25
- (id)initWithUrl:(NSURL *)url
41
[_connection release];
53
if (![NSThread isMainThread])
55
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
59
NSLog(@"operation for <%@> started.", _url);
61
[self willChangeValueForKey:@"isExecuting"];
63
[self didChangeValueForKey:@"isExecuting"];
65
NSURLRequest * request = [NSURLRequest requestWithURL:_url];
66
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
67
[UONetworkStatusCoordinator addNetworkActivity];
68
if (nil == _connection)
76
NSLog(@"operation for <%@> finished. "
77
@"status code: %d, error: %@",
78
_url, _statusCode, _error);
80
[UONetworkStatusCoordinator removeNetworkActivity];
82
[_connection release];
85
[self willChangeValueForKey:@"isExecuting"];
86
[self willChangeValueForKey:@"isFinished"];
91
[self didChangeValueForKey:@"isExecuting"];
92
[self didChangeValueForKey:@"isFinished"];
96
#pragma mark NSURLConnection delegate
98
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
100
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
101
_statusCode = [httpResponse statusCode];
104
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
106
//no-op, subclasses must implement this.
109
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
114
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
116
_error = [error copy];