-
Notifications
You must be signed in to change notification settings - Fork 5
/
WSAsyncURL.m
120 lines (96 loc) · 3.22 KB
/
WSAsyncURL.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// WSAsyncURL.m
// hexcolorpicker
//
// Created by Jesper on 2010-01-01.
// This file is available under terms equivalent to the US public domain.
// For more information, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//
#import "WSAsyncURL.h"
@interface WSAsyncURL ()
- (id)initAsyncWithURL:(NSURL *)url loadDelegate:(id)delegate successSelector:(SEL)successSelector failSelector:(SEL)failSelector;
@end
@implementation WSAsyncURL
static NSMutableSet *wsasyncurlInstances = nil;
- (id)initAsyncWithURL:(NSURL *)aURL loadDelegate:(id)aDelegate successSelector:(SEL)aSuccessSelector failSelector:(SEL)aFailSelector {
if (!aURL) return nil;
if (!aDelegate) return nil;
self = [super init];
if (self) {
url = [aURL copy];
delegate = [aDelegate retain];
successSelector = aSuccessSelector;
failSelector = aFailSelector;
}
return self;
}
/* needed to work in both refcount and GC */
+ (void)retainObject:(WSAsyncURL *)obj {
// NSLog(@"retaining %@ in %@", obj, wsasyncurlInstances);
[wsasyncurlInstances addObject:obj];
}
- (void)retainOurselves {
[WSAsyncURL retainObject:self];
}
+ (void)releaseObject:(WSAsyncURL *)obj {
// NSLog(@"releasing %@ from %@", obj, wsasyncurlInstances);
[wsasyncurlInstances removeObject:obj];
}
- (void)releaseOurselves {
[WSAsyncURL releaseObject:self];
}
- (void)startFetching {
[self retainOurselves];
NSURLRequest *updateReq = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:20];
connection = [[NSURLConnection connectionWithRequest:updateReq delegate:self] retain];
if ([connection respondsToSelector:@selector(start)]) {
[connection performSelector:@selector(start)];
}
// NSLog(@"started connection %@ to %@", connection, url);
if (!connection) {
// NSLog(@"didn't work, fail");
[delegate performSelector:failSelector withObject:nil];
[self releaseOurselves];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (chunk) {
[chunk release];
}
chunk = [[NSMutableData alloc] init];
// NSLog(@"received response");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[chunk appendData:data];
// NSLog(@"append %d bytes", [data length]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
// NSLog(@"finished");
[delegate performSelector:successSelector withObject:[chunk autorelease]];
[self releaseOurselves]; // we're spent!
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (chunk) {
[chunk release];
}
// NSLog(@"failed");
[delegate performSelector:failSelector withObject:[[error copy] autorelease]];
[self releaseOurselves]; // we're spent!
}
- (void)dealloc {
[connection release];
[delegate release];
[url release];
[super dealloc];
}
+ (void)fetchURL:(NSURL *)url loadDelegate:(id)delegate successSelector:(SEL)successSelector failSelector:(SEL)failSelector {
if (!wsasyncurlInstances) {
wsasyncurlInstances = [[NSMutableSet alloc] init];
}
WSAsyncURL *instance = [[WSAsyncURL alloc] initAsyncWithURL:url loadDelegate:delegate successSelector:successSelector failSelector:failSelector];
[instance startFetching];
[instance release];
}
@end