9
9
// adapted from http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/
11
#import "ImageLoadingOperation.h"
13
@interface ImageLoadingOperation ()
17
@implementation ImageLoadingOperation
11
#import "AlbumArtLoadingOperation.h"
13
@implementation AlbumArtLoadingOperation
18
14
@synthesize delegate;
19
@synthesize url = _url;
20
@synthesize statusCode = _statusCode;
21
15
@synthesize data = _data;
22
@synthesize error = _error;
23
@synthesize isExecuting = _isExecuting;
24
@synthesize isFinished = _isFinished;
26
+ (id)imageLoaderWithUrlString:(NSString *)urlString
17
+ (id)AlbumArtLoaderWithUrlString:(NSString *)urlString
28
19
NSURL * url = [NSURL URLWithString:urlString];
29
ImageLoadingOperation * operation = [[self alloc] initWithUrl:url];
20
AlbumArtLoadingOperation * operation = [[self alloc] initWithUrl:url];
30
21
return [operation autorelease];
33
- (id)initWithUrl:(NSURL *)url
24
- (id)initWithArtId:(NSString *)artId URL:(NSURL *)url
26
self = [super initWithUrl:url];
30
_artId = [artId retain];
49
[_connection release];
62
if (![NSThread isMainThread])
64
[self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
68
NSLog(@"opeartion for <%@> started.", _url);
70
[self willChangeValueForKey:@"isExecuting"];
72
[self didChangeValueForKey:@"isExecuting"];
74
NSURLRequest * request = [NSURLRequest requestWithURL:_url];
75
_connection = [[NSURLConnection alloc] initWithRequest:request
77
if (_connection == nil)
83
NSLog(@"operation for <%@> finished. "
84
@"status code: %d, error: %@, data size: %u",
85
_url, _statusCode, _error, [_data length]);
87
[_connection release];
90
[self willChangeValueForKey:@"isExecuting"];
91
[self willChangeValueForKey:@"isFinished"];
96
[self didChangeValueForKey:@"isExecuting"];
97
[self didChangeValueForKey:@"isFinished"];
101
43
#pragma mark NSURLConnection delegate
103
- (void)connection:(NSURLConnection *)connection
104
didReceiveResponse:(NSURLResponse *)response
45
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
47
[super connection:connection didReceiveResponse:response];
107
50
_data = [[NSMutableData alloc] init];
109
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;
110
_statusCode = [httpResponse statusCode];
113
- (void)connection:(NSURLConnection *)connection
114
didReceiveData:(NSData *)data
53
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
55
[super connection:connection didReceiveData:data];
116
57
[_data appendData:data];
119
60
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
121
UIImage *image = [[UIImage alloc] initWithData:_data];
62
UIImage *image = [[[UIImage alloc] initWithData:_data] autorelease];
124
[self.delegate imageLoaded:image forURL:_url];
65
[self.delegate imageLoaded:image forArtId:_artId];
67
[super connectionDidFinishLoading:connection];
129
- (void)connection:(NSURLConnection *)connection
130
didFailWithError:(NSError *)error
70
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
132
_error = [error copy];
72
[super connection:connection didFailWithError:error];