2011-05-17 33 views
0

我開發的應用程序可以讓你從列表中下載播客(在tableview中)。我使用ASINetworkQueue來允許多個同時下載。 的下載位於另一個類發生(另一視圖 - 控制在一個的TabBar應用程序) 這個類是通過調用通知「addRequest設置:(URL)」ASINetworkQueue中的第一個請求總是失敗

我現在面臨的問題是,我創建一個新的隊列,如果ASINetworkQueue爲零,並且我正在添加一個新請求(僅在第一個請求上調用[queue go])

但是,我的第一個請求永遠不會啓動。我在「requestDidStart」中有一個NSLog,並且我看到這沒有被調用。

我的第二個請求,一切正常,但第一個仍然不開始下載。

我的代碼如下:

-(void)downloadFile:(NSMutableDictionary*)objectDetails fromFeed:(NSString*)feedTitle; 
{ 

NSMutableDictionary *fileData = [objectDetails mutableCopy]; 
[fileData setObject:feedTitle forKey:@"fromFeed"]; 

if(networkQueue==nil) 
{ 
    NSLog(@"------------------>Network queue was NIL. Creating...<------------------"); 
    self.networkQueue = [ASINetworkQueue queue]; 
    [self.networkQueue setDelegate:self]; 
    [self.networkQueue setRequestDidFinishSelector:@selector(requestFinished:)]; 
    [self.networkQueue setRequestDidStartSelector:@selector(requestDidStart:)]; 
    [self.networkQueue setRequestDidFailSelector:@selector(requestFailed:)]; 
    [self.networkQueue setQueueDidFinishSelector:@selector(queueFinished:)]; 
} 



ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:[objectDetails objectForKey:@"audio"]]]; 
if (request == nil) { 
    NSLog(@"There was an error downloading the file"); 
    return; 
} 
[request setShouldContinueWhenAppEntersBackground:YES]; 
[request setDelegate:self]; 

//Find pathname for file. 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"Downloads"]; 

//If Downloads folder doesnt exist, create it. 
NSError *error; 
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; 

NSMutableString *filepath = [NSString stringWithFormat:@"%@/%@", 
          dataPath,[[objectDetails objectForKey:@"audio"] lastPathComponent]]; 
if([filepath pathExtension] == @"") 
{ 
    NSLog(@"extension did not exist. assuming MP3"); 
    [filepath appendString:@".mp3"]; 
} 

NSLog(@"Will attempt to download to \n%@",filepath); 

[request setDownloadDestinationPath:filepath]; 
[request setShowAccurateProgress:YES]; 
[networkQueue setShowAccurateProgress:YES]; 
[request setDownloadProgressDelegate:self]; 


[fileData setObject:request forKey:@"request"]; 
[fileData setObject:[NSNumber numberWithLongLong:0.0] forKey:@"completed"]; 

//Add this to array that keeps track of downloads 
if(!pendingDownloadsArray) 
{ 

    pendingDownloadsArray = [[NSMutableArray alloc] init]; 

} 
[pendingDownloadsArray addObject:fileData]; 
[fileData release]; 


[networkQueue addOperation:request]; 
//Add the request to the queue and start. 
if ([networkQueue requestsCount]==1) { 
    NSLog(@"------------------>>Only 1 request. Starting queue with %d objects<------------------",[networkQueue requestsCount]); 
    [networkQueue go]; 
} 
} 

回答

0

你不需要檢查requestcount

只是用 「networkqueue走出去」 是不夠的。

相關問題