-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAsyncRequest.m
More file actions
89 lines (69 loc) · 1.78 KB
/
AsyncRequest.m
File metadata and controls
89 lines (69 loc) · 1.78 KB
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
//
// AsyncRequest.m
// readinglist
//
// Created by Akshay Jawharker on 3/15/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//
#import "AsyncRequest.h"
@implementation AsyncRequest
@synthesize responseData, requestURL, completionBlock, failureBlock;
- (id)init
{
self = [super init];
if (self) {
}
return self;
}
-(void) dealloc
{
[completionBlock release];
[failureBlock release];
[responseData release];
[super dealloc];
}
-(void) requestWithURL:(NSString*) urlString
{
requestURL = [NSURL URLWithString:urlString];
}
-(void) startAsync
{
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:requestURL
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
connection = [ [ NSURLConnection alloc] initWithRequest:urlRequest delegate:self ];
[urlRequest release];
}
-(void) cancelAsync
{
[connection cancel];
[connection release];
}
-(void) setOnCompleteBlock:(CompletionBlock)completeBlock
{
self.completionBlock = completeBlock;
}
-(void) setOnFailureBlock:(FailedBlock)failedBlock
{
self.failureBlock = failedBlock;
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[connection release];
self.failureBlock();
}
//Request FAILED
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
if( responseData == nil ){
responseData = [ [ NSMutableData alloc ] initWithCapacity:2048 ];
}
[responseData appendData : data];
}
//Request COMPLETED
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[connection release];
self.completionBlock();
}
@end