开发者

ASIHTTPRequest Asynchronous Download not canceling

开发者 https://www.devze.com 2023-04-09 07:09 出处:网络
I am trying to cancel an Asynchronous Download using ASIHTTPRequest with no joy. I am downloading a movie to disk on the appearance of a ViewController. What I want is when the user click close to dis

I am trying to cancel an Asynchronous Download using ASIHTTPRequest with no joy. I am downloading a movie to disk on the appearance of a ViewController. What I want is when the user click close to dismiss the view controller I want to cancel the download. Here is the code I have so far:

Making the request:

-(void)retrieveLatestFile{

NSURL *url = [NSURL URLWithString:[appDelegate.urlToLoad stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

ashiRequest = [[ASIHTTPRequest requestWithURL:url] retain];
[ashiRequest setUsername:@"Appidae"];
[ashiRequest setPassword:@"Dubstance1"];
[ashiRequest setDelegate:self];
[ashiRequest startAsynchronous];
[ashiRequest setDownloadProgressDelegate:progressView];
NSLog(@"Value: %f", [progressView progress]);
if ([progressView progress]==1) {
    [self hideInfoPane];
}
//NSLog(@"Max: %f, Value: %f", [myProgressIndicator maxValue],[myProgressIndicator doubleValue]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *tempP开发者_Python百科ath = [NSString stringWithFormat:@"%@/download/%@/%@", documentsDirectory, appDelegate.languageFolder,appDelegate.filename];


NSLog(@"Path: %@", tempPath);

[ashiRequest setDownloadDestinationPath:tempPath];
}

Cancel the the downloading:

- (IBAction)closeDocDisplay:(id)sender {
// Cancels an asynchronous request


[ashiRequest clearDelegatesAndCancel];


[self dismissModalViewControllerAnimated:YES];
}

Everything is working, the download is fine and the movie plays properly but when I dismiss the View Controller the request is not canceled and my file is still downloading (network activity indicator spins on the status bar. Even the ASIHTTP delegate error doesn't get called


Try using

ashiRequest.delegate = nil; 
[ashiRequest cancel];

This should work.


I figured out what was wrong. I was using Reachability to call my retrieveLatestFileMethod. That's fine if Reachability is being used just once in the app, maybe from the delegate. But I had a few instances of it so it keept calling the same functions and downloads.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号