Skip to content

Commit

Permalink
implement cheaper version of request id
Browse files Browse the repository at this point in the history
  • Loading branch information
nor committed Sep 13, 2012
1 parent dc1bf30 commit 5bdba71
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions Classes/BDMultiDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ - (id)copyWithZone:(NSZone *)zone


#pragma mark - NSURLRequest Extension
#define BDURLRequestRequestIdKey @"BDURLRequestRequestIdKey"

@interface NSURLRequest (BDMultiDownloader)
- (NSString*)requestId;
@end
Expand All @@ -79,10 +81,11 @@ @implementation NSURLRequest (BDMultiDownloader)

-(NSString *)requestId
{
if ([[self.HTTPMethod uppercaseString] isEqualToString:BDMultiDownloaderMethodPOST]) {
return [NSString stringWithFormat:@"%@%@%@",self.URL.absoluteString, self.HTTPMethod, self.HTTPBody];
}
return self.URL.absoluteString;
// if ([[self.HTTPMethod uppercaseString] isEqualToString:BDMultiDownloaderMethodPOST]) {
// return [NSString stringWithFormat:@"%@%@%@",self.URL.absoluteString, self.HTTPMethod, self.HTTPBody];
// }
// return self.URL.absoluteString;
return [(NSMutableURLRequest*)self valueForHTTPHeaderField:BDURLRequestRequestIdKey];
}
@end

Expand All @@ -108,7 +111,19 @@ - (NSUInteger) numberOfItemsInQueue;
@end

@implementation BDMultiDownloader
static NSUInteger requestId;

- (void)_addRequestId:(NSURLRequest**)request
{
if (![*request isKindOfClass:[NSMutableURLRequest class]]) {
*request = [(*request) mutableCopy];
}

//add request id before sending off
NSNumber * rid = [NSNumber numberWithInt:(requestId++)];
[(NSMutableURLRequest*) *request addValue:rid.stringValue forHTTPHeaderField:BDURLRequestRequestIdKey];

}

- (void)queueRequest:(NSString *)urlPath completion:(void (^)(NSData *))completionWithDownloadedData
{
Expand Down Expand Up @@ -145,6 +160,7 @@ - (void)queueRequest:(NSString *)urlPath completion:(void (^)(NSData *))completi
request = [NSURLRequest requestWithURL:url cachePolicy:cachePolicy timeoutInterval:timeout];
}

[self _addRequestId:&request];
if (request){
[_loadingQueue addObject:request];
[_requestCompletions setObject:[completionWithDownloadedData copy] forKey:request.requestId];
Expand All @@ -154,6 +170,8 @@ - (void)queueRequest:(NSString *)urlPath completion:(void (^)(NSData *))completi

- (void)queueURLRequest:(NSURLRequest *)urlRequest completion:(void (^)(NSData *))completionWithDownloadedData
{
[self _addRequestId:&urlRequest];

[_loadingQueue addObject:[urlRequest copy] ];
[_requestCompletions setObject:[completionWithDownloadedData copy] forKey:urlRequest.requestId];
[self launchNextConnection];
Expand Down

0 comments on commit 5bdba71

Please sign in to comment.