[英]First request in ASINetworkQueue always fails
Im developing an app that lets you download podcasts from a list (in a tableview). 我正在开发一个应用程序,可让您从列表(在表视图中)下载播客。 Im using ASINetworkQueue to allow multiple simultaneous downloads.
我正在使用ASINetworkQueue允许多个同时下载。 The downloads are happening in another class (another viewcontroller in a tabbar application) and this class is notified by a call "addRequest:(url)"
下载发生在另一个类中(标签栏应用程序中的另一个viewcontroller),并且通过调用“ addRequest:(url)”通知该类。
The problem I am facing is that I am creating a new queue, if the ASINetworkQueue is nil, and I am adding a new request, (calling [queue go] only on the first one) 我面临的问题是,如果ASINetworkQueue为nil,我正在创建一个新队列,并且正在添加一个新请求,(仅在第一个请求上调用[queue go])
But, my first request never starts. 但是,我的第一个请求永远不会开始。 I have an NSLog in the "requestDidStart", and I see that this is not getting called.
我在“ requestDidStart”中有一个NSLog,看到没有被调用。
My second request onwards, everything works fine, but the first one still doesnt start downloading. 从我的第二个请求开始,一切正常,但是第一个仍然没有开始下载。
My code is below: 我的代码如下:
-(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];
}
}
You dont need check requestcount 您不需要支票请求数
just use "networkqueue go" is enough. 只需使用“ networkqueue go”就足够了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.