ASIHTTPRequest异步下载未取消

发布于 2024-12-07 03:22:29 字数 1455 浏览 2 评论 0原文

我试图取消使用 ASIHTTPRequest 的异步下载,但没有任何乐趣。我正在 ViewController 的外观上将电影下载到磁盘。我想要的是当用户单击关闭以关闭视图控制器时我想取消下载。这是我到目前为止的代码:

发出请求:

-(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 *tempPath = [NSString stringWithFormat:@"%@/download/%@/%@", documentsDirectory, appDelegate.languageFolder,appDelegate.filename];


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

[ashiRequest setDownloadDestinationPath:tempPath];
}

取消下载:

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


[ashiRequest clearDelegatesAndCancel];


[self dismissModalViewControllerAnimated:YES];
}

一切正常,下载正常,电影播放正常,但是当我关闭视图控制器时,请求没有取消,我的文件仍在下载(网络活动指示器在状态栏上旋转,甚至 ASIHTTP 委托错误也不会被调用。

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 *tempPath = [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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-12-14 03:22:29

尝试使用

ashiRequest.delegate = nil; 
[ashiRequest cancel];

这应该可以。

Try using

ashiRequest.delegate = nil; 
[ashiRequest cancel];

This should work.

肤浅与狂妄 2024-12-14 03:22:29

我知道出了什么问题了。我正在使用 Reachability 来调用我的retrieveLatestFileMethod。如果 Reachability 在应用程序中仅使用一次(可能是从委托中使用),那就没问题。但我有几个它的实例,所以它不断调用相同的函数和下载。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文