Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

clean up private data

  • Loading branch information...
commit bf05b5ea0f003a5b057dcbd78a2204d77e252706 1 parent 1c69fd1
nor authored
Showing with 62 additions and 45 deletions.
  1. +62 −45 Classes/BDMultiDownloader.m
View
107 Classes/BDMultiDownloader.m
@@ -34,7 +34,14 @@
#import "BDMultiDownloader.h"
-#define kIntervalDefaultTimeout 60
+#ifdef BDMD_DEBUG
+# define BDMDLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
+#else
+# define BDMDLog(...)
+#endif
+
+
+#define kIntervalDefaultTimeout 30
#define kMaxNumberOfThreads 25
#define kMaxCache 10 * 1024 * 1000
@@ -47,7 +54,7 @@
@interface BDURLConnection : NSURLConnection{
void(^_completion)(NSData*);
}
-@property (nonatomic, copy) void(^completionWithDownloadedData)(NSData*);
+
@property (nonatomic, assign) double progress;
@property (nonatomic, assign) long long expectedLength;
@property (nonatomic, strong) NSString *MIMEType;
@@ -61,7 +68,7 @@ - (id)copyWithZone:(NSZone *)zone
{
return self;
}
-@synthesize completionWithDownloadedData;
+
@synthesize MIMEType;
@synthesize expectedLength;
@synthesize progress;
@@ -81,17 +88,24 @@ @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;
- return [(NSMutableURLRequest*)self valueForHTTPHeaderField:BDURLRequestRequestIdKey];
+ NSMutableString * rid = [NSMutableString stringWithString:@""];
+ [rid appendFormat:@"%@", self.URL.absoluteString];
+ if (self.HTTPBody.length > 0) {
+ NSString* body = [[NSString alloc] initWithData:self.HTTPBody encoding:NSUTF8StringEncoding];
+ [rid appendFormat:@"%d", body.hash];
+ }
+
+ return rid;
}
@end
#pragma mark - BDMultiDownloader implementations
@interface BDMultiDownloader ()
{
+
+ NSUInteger _numPutInQueue;
+ NSUInteger _numOutFromQueue;
+
//class state data
NSMutableArray *_currentConnections;
NSMutableDictionary *_currentConnectionsData; //map NSURLConnection to NSData
@@ -111,19 +125,6 @@ - (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
{
@@ -160,7 +161,6 @@ - (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];
@@ -170,9 +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] ];
+ [_loadingQueue addObject:urlRequest];
[_requestCompletions setObject:[completionWithDownloadedData copy] forKey:urlRequest.requestId];
[self launchNextConnection];
}
@@ -205,7 +204,10 @@ - (void)jsonWithRequest:(NSURLRequest *)jsonRequest options:(NSJSONWritingOption
- (void)imageWithPath:(NSString *)urlPath completion:(void (^)(UIImage *, BOOL))completionWithImageYesIfFromCache
{
- NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]];
+ NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]
+ cachePolicy:self.urlCacheStoragePolicy
+ timeoutInterval:self.connectionTimeout];
+
NSData *data = [_dataCache objectForKey:request.requestId];
if (data.length > 0){
UIImage *image = [UIImage imageWithData:data];
@@ -243,7 +245,7 @@ - (void)clearCache
- (void)clearQueue
{
- //DLog(@"clear queue.");
+ BDMDLog(@"clear queue.");
for (NSURLConnection *conn in _currentConnections) {
if (self.onNetworkActivity) {
@@ -256,6 +258,9 @@ - (void)clearQueue
[_loadingQueue removeAllObjects];
[_requestCompletions removeAllObjects];
[_currentConnectionsData removeAllObjects];
+ _numOutFromQueue = 0;
+ _numPutInQueue = 0;
+
}
- (NSUInteger)numberOfItemsInQueue
@@ -265,23 +270,23 @@ - (NSUInteger)numberOfItemsInQueue
- (void)launchNextConnection
{
- //DLog(@"launchNextConnection…");
if (self.pause){
return;
}
if (_currentConnections.count >= self.maximumNumberOfThreads) {
-// DLog(@"Threads at Max. Abort.");
+ BDMDLog(@"Threads at Max. Abort. (%d > %d)", _currentConnections.count, self.maximumNumberOfThreads);
+ BDMDLog(@"launcehd %d, responded %d", _numPutInQueue, _numOutFromQueue);
return;
}
if (self.numberOfItemsInQueue==0) {
-// DLog(@"Nothing in queue.");
return;
}
-// DLog(@"still in queue: %d", self.numberOfItemsInQueue);
+ BDMDLog(@"launcehd %d, responded %d", _numPutInQueue, _numOutFromQueue);
+ _numPutInQueue++;
NSURLRequest *request = [_loadingQueue objectAtIndex:0];
[_loadingQueue removeObjectAtIndex:0];
@@ -300,9 +305,7 @@ - (void)launchNextConnection
conn.originalRequest = request;
conn.suggestedFilename = request.URL.lastPathComponent;
[_currentConnections addObject:conn];
-
- void (^completion)(NSData*) = [_requestCompletions objectForKey:requestKey];
- [conn setCompletionWithDownloadedData:completion];
+
[conn start];
if (self.onNetworkActivity) {
self.onNetworkActivity(YES);
@@ -321,7 +324,7 @@ - (void)setCacheSizeLimit:(NSUInteger)cacheSizeLimit
#pragma mark - connection delegate
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSMutableData *data = [[NSMutableData alloc] init];
- [_currentConnectionsData setObject:data forKey:connection];
+ [_currentConnectionsData setObject:data forKey:connection.originalRequest.requestId];
BDURLConnection *conn = (BDURLConnection*) connection;
[conn setMIMEType:response.MIMEType];
@@ -335,7 +338,7 @@ -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLRespons
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
- NSMutableData *_data = (NSMutableData*)[_currentConnectionsData objectForKey:connection];
+ NSMutableData *_data = (NSMutableData*)[_currentConnectionsData objectForKey:connection.originalRequest.requestId];
[_data appendData:data];
BDURLConnection *conn = (BDURLConnection*) connection;
[conn setProgress:_data.length/(double) conn.expectedLength ];
@@ -348,25 +351,30 @@ - (void)_removeConnection:(BDURLConnection*)conn
{
__block NSUInteger indexToRemove = NSNotFound;
+
+ NSString* requestId = conn.originalRequest.requestId;
+ NSAssert(requestId!=nil, @"requestId is nil. Not good.");
[_loadingQueue enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSURLRequest *req = obj;
- if ([req.URL.absoluteString isEqualToString:conn.originalRequest.URL.absoluteString]){
+
+ if ([req.requestId isEqual:requestId]){
indexToRemove = idx;
*stop = YES;
}
}];
-
-
if (indexToRemove!=NSNotFound) {
[_loadingQueue removeObjectAtIndex:indexToRemove];
}
[_currentConnections removeObject:conn];
- [_currentConnectionsData removeObjectForKey:conn];
- [_requestCompletions removeObjectForKey:conn];
+ [_currentConnectionsData removeObjectForKey:requestId];
+ [_requestCompletions removeObjectForKey:requestId];
+ _numOutFromQueue++;
+ BDMDLog(@"launcehd %d, responded %d", _numPutInQueue, _numOutFromQueue);
+
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
@@ -375,25 +383,34 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection
self.onNetworkActivity(NO);
}
- NSData *data = [_currentConnectionsData objectForKey:connection];
+ NSString *requestId = connection.originalRequest.requestId;
+ NSAssert(requestId!=nil, @"requestId can't be nil.");
+ NSData *data = [_currentConnectionsData objectForKey:requestId];
BDURLConnection *conn = (BDURLConnection*) connection;
- NSString *requestKey = conn.originalRequest.requestId;
+ BDMDLog(@"finished request key: %@ ", requestId);
if (data.length > 0){
- [_dataCache setObject:data forKey:requestKey cost:data.length];
+ [_dataCache setObject:data forKey:requestId cost:data.length];
+ }else{
+ BDMDLog(@"!! zero length data for %@", requestId);
}
- [self _removeConnection:conn];
- void(^completion)(NSData*) = [(BDURLConnection*)connection completionWithDownloadedData];
+ void(^completion)(NSData*) = [_requestCompletions objectForKey:requestId];
if (completion) {
completion(data);
+ }else{
+ BDMDLog(@"%@ doesn't have completion", requestId);
}
+ [self _removeConnection:conn];
+
[self launchNextConnection];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
+ BDMDLog(@"failed %@ %@", connection.originalRequest.URL, error.localizedFailureReason);
+
if (self.onNetworkActivity){
self.onNetworkActivity(NO);
}
Please sign in to comment.
Something went wrong with that request. Please try again.