Skip to content

Commit

Permalink
use originalURL instead of originalPath
Browse files Browse the repository at this point in the history
  • Loading branch information
norsez committed Jul 6, 2012
1 parent 1fa53eb commit a67f3e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
14 changes: 10 additions & 4 deletions Classes/BDMultiDownloader.h
Expand Up @@ -39,10 +39,12 @@
@interface BDMultiDownloader : NSObject <NSURLConnectionDelegate>

/**
* Add a path for data download. Download begins when there's a free connection
* available in the pool. The downloaded data is automatically cached.
* @param urlPath path to download.
* @param completionWithDownloadedData block returned with downloaded data.
Add a path for data download. Download begins when there's a free connection
available in the pool. The downloaded data is automatically cached.
This is the core method of this class.
@param urlPath path to download.
@param completionWithDownloadedData block returned with downloaded data.
*/
- (void) queueRequest:(NSString*)urlPath completion:(void(^)(NSData*))completionWithDownloadedData;

Expand Down Expand Up @@ -94,6 +96,10 @@

#pragma mark - optional configs
@property (nonatomic, assign) NSUInteger cacheSizeLimit;
/**
The number of ongoing downloads at one time. The class holds download requests in a queue, and wait
for there are download slots available before actually start making download connections.
*/
@property (nonatomic, assign) NSUInteger maximumNumberOfThreads;
@property (nonatomic, strong) NSDictionary *httpHeaders;
@property (nonatomic, assign) NSTimeInterval connectionTimeout;
Expand Down
21 changes: 13 additions & 8 deletions Classes/BDMultiDownloader.m
Expand Up @@ -35,12 +35,14 @@
#import "BDMultiDownloader.h"

#define kIntervalDefaultTimeout 60
#define kMaxNumberOfThreads 25
#define kMaxCache 10 * 1024 * 1000
NSString* const kPOST = @"POST";

@interface BDURLConnection : NSURLConnection{
void(^_completion)(NSData*);
}
@property (nonatomic, copy) void(^completionWithDownloadedData)(NSData*);
@property (nonatomic, copy) NSString* originalPath;
@property (nonatomic, assign) double progress;
@property (nonatomic, assign) long long expectedLength;
@property (nonatomic, strong) NSString *MIMEType;
Expand All @@ -54,7 +56,6 @@ - (id)copyWithZone:(NSZone *)zone
return self;
}
@synthesize completionWithDownloadedData;
@synthesize originalPath;
@synthesize MIMEType;
@synthesize expectedLength;
@synthesize progress;
Expand All @@ -76,9 +77,9 @@ - (NSUInteger) numberOfItemsInQueue;
- (NSString*) keyForRequest:(NSURLRequest*)request;
@end

#define kMaxNumberOfThreads 25
#define kMaxCache 10 * 1024 * 1000
@implementation BDMultiDownloader


- (void)queueRequest:(NSString *)urlPath completion:(void (^)(NSData *))completionWithDownloadedData
{
if(!urlPath){
Expand Down Expand Up @@ -156,9 +157,11 @@ - (void)imageWithPath:(NSString *)urlPath completion:(void (^)(UIImage *, BOOL))

- (void)dequeueWithPath:(NSString *)path
{
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:path]];
NSString *key = [self keyForRequest:request];
NSArray * searchResults = [_currentConnections filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
BDURLConnection *aConn = evaluatedObject;
return [aConn.originalPath isEqualToString:path];
return [[self keyForRequest:aConn.originalRequest] isEqualToString:key];
}]];

if (searchResults.count > 0) {
Expand Down Expand Up @@ -195,7 +198,10 @@ - (NSUInteger)numberOfItemsInQueue

- (NSString*) keyForRequest:(NSURLRequest*)request
{
return [NSString stringWithFormat:@"%@%@",request.URL.absoluteString, request.allHTTPHeaderFields.allValues];
if ([[request.HTTPMethod uppercaseString] isEqualToString:kPOST]) {
return [NSString stringWithFormat:@"%@%@%@",request.URL.absoluteString, request.HTTPMethod, request.HTTPBody];
}
return request.URL.absoluteString;
}

- (void)launchNextConnection
Expand Down Expand Up @@ -224,7 +230,6 @@ - (void)launchNextConnection
}

BDURLConnection *conn = [[BDURLConnection alloc] initWithRequest:request delegate:self];
conn.originalPath = request.URL.absoluteString;
[_currentConnections addObject:conn];

void (^completion)(NSData*) = [_requestCompletions objectForKey:[self keyForRequest:request]];
Expand Down Expand Up @@ -281,7 +286,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
NSData *data = [_currentConnectionsData objectForKey:connection];
BDURLConnection *conn = (BDURLConnection*) connection;
if (data.length > 0){
[_dataCache setObject:data forKey:conn.originalPath cost:data.length];
[_dataCache setObject:data forKey:conn.originalRequest cost:data.length];
}
[_currentConnectionsData removeObjectForKey:connection];
[_loadingQueue removeObject:[self keyForRequest:connection.originalRequest]];
Expand Down

0 comments on commit a67f3e4

Please sign in to comment.