2 // OAAsynchronousDataFetcher.m
5 // Created by Zsombor Szabó on 12/3/08.
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #import "OAAsynchronousDataFetcher.h"
27 #import "OAServiceTicket.h"
29 @implementation OAAsynchronousDataFetcher
31 + (id)asynchronousFetcherWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector
33 return [[[OAAsynchronousDataFetcher alloc] initWithRequest:aRequest delegate:aDelegate didFinishSelector:finishSelector didFailSelector:failSelector] autorelease];
36 - (id)initWithRequest:(OAMutableURLRequest *)aRequest delegate:(id)aDelegate didFinishSelector:(SEL)finishSelector didFailSelector:(SEL)failSelector
38 if (self = [super init])
40 request = [aRequest retain];
42 didFinishSelector = finishSelector;
43 didFailSelector = failSelector;
55 connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
60 [responseData release];
61 responseData = [[NSMutableData data] retain];
65 OAServiceTicket *ticket= [[OAServiceTicket alloc] initWithRequest:request
68 [delegate performSelector:didFailSelector
83 if (request) [request release];
84 if (connection) [connection release];
85 if (response) [response release];
86 if (responseData) [responseData release];
91 #pragma mark NSURLConnection's delegate methods
93 - (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse
97 response = [aResponse retain];
98 [responseData setLength:0];
101 - (void)connection:(NSURLConnection *)aConnection didReceiveData:(NSData *)data
103 [responseData appendData:data];
106 - (void)connection:(NSURLConnection *)aConnection didFailWithError:(NSError *)error
108 OAServiceTicket *ticket= [[OAServiceTicket alloc] initWithRequest:request
111 [delegate performSelector:didFailSelector
118 - (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
120 OAServiceTicket *ticket = [[OAServiceTicket alloc] initWithRequest:request
122 didSucceed:[(NSHTTPURLResponse *)response statusCode] < 400];
123 [delegate performSelector:didFinishSelector
125 withObject:responseData];