ASINetworkQueue中的第一个请求始终失败

时间:2011-05-17 06:49:01

标签: iphone download asihttprequest

开发一款可让您从列表中下载播客的应用程序(在tableview中)。 我使用ASINetworkQueue允许多个同时下载。 下载发生在另一个类(tabbar应用程序中的另一个viewcontroller) 并且通过调用“addRequest:(url)”

通知此类

我面临的问题是我正在创建一个新队列,如果ASINetworkQueue为nil,我正在添加一个新请求,(仅在第一个上调用[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];
} 
 }

1 个答案:

答案 0 :(得分:0)

您不需要检查requestcount

只需使用“networkqueue go”即可。