Added [request startAsynchronous] for easy async requests (no queue required!)
Showing
3 changed files
with
91 additions
and
2 deletions
| @@ -343,6 +343,12 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | @@ -343,6 +343,12 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | ||
| 343 | // Returns true if the response was gzip compressed | 343 | // Returns true if the response was gzip compressed |
| 344 | - (BOOL)isResponseCompressed; | 344 | - (BOOL)isResponseCompressed; |
| 345 | 345 | ||
| 346 | +#pragma mark running a request | ||
| 347 | + | ||
| 348 | +// Run a request asynchronously by adding it to the global queue | ||
| 349 | +// (Use [request start] for a synchronous request) | ||
| 350 | +- (void)startAsynchronous; | ||
| 351 | + | ||
| 346 | #pragma mark request logic | 352 | #pragma mark request logic |
| 347 | 353 | ||
| 348 | // Main request loop is in here | 354 | // Main request loop is in here |
| @@ -418,7 +424,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | @@ -418,7 +424,11 @@ extern unsigned long const ASIWWANBandwidthThrottleAmount; | ||
| 418 | - (void)handleStreamComplete; | 424 | - (void)handleStreamComplete; |
| 419 | - (void)handleStreamError; | 425 | - (void)handleStreamError; |
| 420 | 426 | ||
| 421 | -# pragma mark session credentials | 427 | +#pragma mark global queue |
| 428 | + | ||
| 429 | ++ (NSOperationQueue *)sharedRequestQueue; | ||
| 430 | + | ||
| 431 | +#pragma mark session credentials | ||
| 422 | 432 | ||
| 423 | + (NSMutableArray *)sessionProxyCredentialsStore; | 433 | + (NSMutableArray *)sessionProxyCredentialsStore; |
| 424 | + (NSMutableArray *)sessionCredentialsStore; | 434 | + (NSMutableArray *)sessionCredentialsStore; |
| @@ -93,6 +93,8 @@ static NSRecursiveLock *sessionCookiesLock = nil; | @@ -93,6 +93,8 @@ static NSRecursiveLock *sessionCookiesLock = nil; | ||
| 93 | // This is so it can make use of any credentials supplied for the other request, if they are appropriate | 93 | // This is so it can make use of any credentials supplied for the other request, if they are appropriate |
| 94 | static NSRecursiveLock *delegateAuthenticationLock = nil; | 94 | static NSRecursiveLock *delegateAuthenticationLock = nil; |
| 95 | 95 | ||
| 96 | +static NSOperationQueue *sharedRequestQueue = nil; | ||
| 97 | + | ||
| 96 | // Private stuff | 98 | // Private stuff |
| 97 | @interface ASIHTTPRequest () | 99 | @interface ASIHTTPRequest () |
| 98 | 100 | ||
| @@ -403,6 +405,15 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -403,6 +405,15 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 403 | } | 405 | } |
| 404 | } | 406 | } |
| 405 | 407 | ||
| 408 | +#pragma mark running a request | ||
| 409 | + | ||
| 410 | +// Run a request asynchronously by adding it to the global queue | ||
| 411 | +// (Use [request start] for a synchronous request) | ||
| 412 | +- (void)startAsynchronous | ||
| 413 | +{ | ||
| 414 | + [[ASIHTTPRequest sharedRequestQueue] addOperation:self]; | ||
| 415 | +} | ||
| 416 | + | ||
| 406 | 417 | ||
| 407 | #pragma mark request logic | 418 | #pragma mark request logic |
| 408 | 419 | ||
| @@ -2088,6 +2099,17 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | @@ -2088,6 +2099,17 @@ static NSRecursiveLock *delegateAuthenticationLock = nil; | ||
| 2088 | [super cancel]; | 2099 | [super cancel]; |
| 2089 | } | 2100 | } |
| 2090 | 2101 | ||
| 2102 | +#pragma mark global queue | ||
| 2103 | + | ||
| 2104 | ++ (NSOperationQueue *)sharedRequestQueue | ||
| 2105 | +{ | ||
| 2106 | + if (!sharedRequestQueue) { | ||
| 2107 | + sharedRequestQueue = [[NSOperationQueue alloc] init]; | ||
| 2108 | + [sharedRequestQueue setMaxConcurrentOperationCount:YES]; | ||
| 2109 | + } | ||
| 2110 | + return sharedRequestQueue; | ||
| 2111 | +} | ||
| 2112 | + | ||
| 2091 | # pragma mark session credentials | 2113 | # pragma mark session credentials |
| 2092 | 2114 | ||
| 2093 | + (NSMutableArray *)sessionProxyCredentialsStore | 2115 | + (NSMutableArray *)sessionProxyCredentialsStore |
| @@ -923,7 +923,64 @@ | @@ -923,7 +923,64 @@ | ||
| 923 | GHAssertTrue(success,@"Got wrong response status message"); | 923 | GHAssertTrue(success,@"Got wrong response status message"); |
| 924 | } | 924 | } |
| 925 | 925 | ||
| 926 | +- (void)testAsynchronousWithGlobalQueue | ||
| 927 | +{ | ||
| 928 | + ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/first"]]; | ||
| 929 | + [request setUserInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:@"RequestNumber"]]; | ||
| 930 | + [request setDidFailSelector:@selector(asyncFail:)]; | ||
| 931 | + [request setDidFinishSelector:@selector(asyncSuccess:)]; | ||
| 932 | + [request setDelegate:self]; | ||
| 933 | + [request startAsynchronous]; | ||
| 934 | + | ||
| 935 | + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/second"]]; | ||
| 936 | + [request setUserInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:2] forKey:@"RequestNumber"]]; | ||
| 937 | + [request setDidFailSelector:@selector(asyncFail:)]; | ||
| 938 | + [request setDidFinishSelector:@selector(asyncSuccess:)]; | ||
| 939 | + [request setDelegate:self]; | ||
| 940 | + [request startAsynchronous]; | ||
| 941 | + | ||
| 942 | + request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/tests/third"]]; | ||
| 943 | + [request setUserInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:3] forKey:@"RequestNumber"]]; | ||
| 944 | + [request setDidFailSelector:@selector(asyncFail:)]; | ||
| 945 | + [request setDidFinishSelector:@selector(asyncSuccess:)]; | ||
| 946 | + [request setDelegate:self]; | ||
| 947 | + [request startAsynchronous]; | ||
| 948 | + | ||
| 949 | + request = [ASIHTTPRequest requestWithURL:nil]; | ||
| 950 | + [request setUserInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:4] forKey:@"RequestNumber"]]; | ||
| 951 | + [request setDidFailSelector:@selector(asyncFail:)]; | ||
| 952 | + [request setDidFinishSelector:@selector(asyncSuccess:)]; | ||
| 953 | + [request setDelegate:self]; | ||
| 954 | + [request startAsynchronous]; | ||
| 955 | +} | ||
| 926 | 956 | ||
| 927 | -@end | 957 | + |
| 958 | +- (void)asyncFail:(ASIHTTPRequest *)request | ||
| 959 | +{ | ||
| 960 | + int requestNumber = [[[request userInfo] objectForKey:@"RequestNumber"] intValue]; | ||
| 961 | + GHAssertEquals(requestNumber,4,@"Wrong request failed"); | ||
| 962 | +} | ||
| 963 | + | ||
| 964 | +- (void)asyncSuccess:(ASIHTTPRequest *)request | ||
| 965 | +{ | ||
| 966 | + int requestNumber = [[[request userInfo] objectForKey:@"RequestNumber"] intValue]; | ||
| 967 | + GHAssertNotEquals(requestNumber,4,@"Request succeeded when it should have failed"); | ||
| 968 | + | ||
| 969 | + BOOL success; | ||
| 970 | + switch (requestNumber) { | ||
| 971 | + case 1: | ||
| 972 | + success = [[request responseString] isEqualToString:@"This is the expected content for the first string"]; | ||
| 973 | + break; | ||
| 974 | + case 2: | ||
| 975 | + success = [[request responseString] isEqualToString:@"This is the expected content for the second string"]; | ||
| 976 | + break; | ||
| 977 | + case 3: | ||
| 978 | + success = [[request responseString] isEqualToString:@"This is the expected content for the third string"]; | ||
| 979 | + break; | ||
| 980 | + } | ||
| 981 | + GHAssertTrue(success,@"Got wrong request content - very bad!"); | ||
| 982 | + | ||
| 983 | +} | ||
| 928 | 984 | ||
| 929 | 985 | ||
| 986 | +@end |
-
Please register or login to post a comment