ASINetworkQueue 中的第一个请求总是失败
我正在开发一个应用程序,可让您从列表(在表格视图中)下载播客。 我使用 ASINetworkQueue 来允许同时进行多个下载。 下载发生在另一个类中(选项卡栏应用程序中的另一个视图控制器) 这个类是通过调用“addRequest:(url)”来通知的。
我面临的问题是,如果 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];
}
}
Im developing an app that lets you download podcasts from a list (in a tableview).
Im using ASINetworkQueue to allow multiple simultaneous downloads.
The downloads are happening in another class (another viewcontroller in a tabbar application)
and this class is notified by a call "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)
But, my first request never starts. I have an NSLog in the "requestDidStart", and I see that this is not getting called.
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];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要检查请求计数,
只需使用“networkqueue go”就足够了。
You dont need check requestcount
just use "networkqueue go" is enough.