如何恢复 ASINetworkQueue?

发布于 2024-12-14 04:43:43 字数 3823 浏览 0 评论 0原文

我正在尝试做的是从网络连接丢失时的同一点恢复和排队。我的问题不在于可达性,而在于恢复下载。我已经度过了整个周末,但运气不佳。
非常感谢任何帮助。

下面的代码具有方法 commenceDownloadIfReachabilityIsOkay ,当网络恢复但进度条不移动时,该方法会被调用,并且我没有看到任何活动来判断下载是否已恢复。

此方法在控制器中调用 frin IBAction

 - (void) downloadFile:(UIProgressView*)locprogressView;
    {

    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [hostReachable startNotifier];

        if (![self asiqueue]) {
            [self setAsiqueue:[[[ASINetworkQueue alloc] init] autorelease]];
        }

     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];

        self.request = [ASIHTTPRequest requestWithURL:url];
        _progressView=locprogressView;

        self.progressView= locprogressView;

        [asiqueue cancelAllOperations];
        [asiqueue setDownloadProgressDelegate:self.progressView];
        [asiqueue setDelegate:self];
        [asiqueue setRequestDidFinishSelector:@selector(queueComplete:)];
        [asiqueue setShowAccurateProgress:YES];
        // [queue setr

        [request setDelegate:self];
        // [request setDidReceiveDataSelector:@selector(didReceiveData:)];
        [request setDidFinishSelector:@selector(requestDone:)];
        [request setDidFailSelector:@selector(requestWentWrong:)];
        [request setAllowResumeForFileDownloads:YES];
        [[self asiqueue] addOperation:request]; //queue is an NSOperationQueue
        [[self asiqueue]go ];

}

此方法从 checkNetworkStatus 调用,我希望它恢复下载

 - (void)commenceDownloadIfReachabilityIsOkay
    {
        if (self.asiqueue && self.hostActive && self.internetActive)
        {

            [[self asiqueue]go ];

        }

    }

此方法只是不断轮询检查网络状态 - 取自如何检查对于一个活跃的iOS 或 OSX 上有互联网连接吗?

- (void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.internetActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;

            break;

        }
    }

    [self commenceDownloadIfReachabilityIsOkay];
}

What I am trying to do it to resume and queue from the same point as where in was when network connection was lost . My problem is not with reachability but rather resumption of download. I have spend whole weekend but have had no luck .
Would hugely appreciate any help.

Code below has method commenceDownloadIfReachabilityIsOkay which does get called when network is resumes but progress bar does not move and I do not see any activity to tell if download has resume.

This method gets called frin IBAction in controller

 - (void) downloadFile:(UIProgressView*)locprogressView;
    {

    // check for internet connection
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];

    internetReachable = [[Reachability reachabilityForInternetConnection] retain];
    [internetReachable startNotifier];

    // check if a pathway to a random host exists
    hostReachable = [[Reachability reachabilityWithHostName: @"www.apple.com"] retain];
    [hostReachable startNotifier];

        if (![self asiqueue]) {
            [self setAsiqueue:[[[ASINetworkQueue alloc] init] autorelease]];
        }

     NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com/ASIHTTPRequest/Tests/the_great_american_novel.txt"];

        self.request = [ASIHTTPRequest requestWithURL:url];
        _progressView=locprogressView;

        self.progressView= locprogressView;

        [asiqueue cancelAllOperations];
        [asiqueue setDownloadProgressDelegate:self.progressView];
        [asiqueue setDelegate:self];
        [asiqueue setRequestDidFinishSelector:@selector(queueComplete:)];
        [asiqueue setShowAccurateProgress:YES];
        // [queue setr

        [request setDelegate:self];
        // [request setDidReceiveDataSelector:@selector(didReceiveData:)];
        [request setDidFinishSelector:@selector(requestDone:)];
        [request setDidFailSelector:@selector(requestWentWrong:)];
        [request setAllowResumeForFileDownloads:YES];
        [[self asiqueue] addOperation:request]; //queue is an NSOperationQueue
        [[self asiqueue]go ];

}

This method gets called from checkNetworkStatus and I want it to resume download

 - (void)commenceDownloadIfReachabilityIsOkay
    {
        if (self.asiqueue && self.hostActive && self.internetActive)
        {

            [[self asiqueue]go ];

        }

    }

This method just keeps polling to check network status - Taken from How to check for an active Internet connection on iOS or OSX?

- (void) checkNetworkStatus:(NSNotification *)notice
{
    // called after network status changes

    NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
    switch (internetStatus)

    {
        case NotReachable:
        {
            NSLog(@"The internet is down.");
            self.internetActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"The internet is working via WIFI.");
            self.internetActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"The internet is working via WWAN.");
            self.internetActive = YES;

            break;

        }
    }

    NetworkStatus hostStatus = [hostReachable currentReachabilityStatus];
    switch (hostStatus)

    {
        case NotReachable:
        {
            NSLog(@"A gateway to the host server is down.");
            self.hostActive = NO;

            break;

        }
        case ReachableViaWiFi:
        {
            NSLog(@"A gateway to the host server is working via WIFI.");
            self.hostActive = YES;

            break;

        }
        case ReachableViaWWAN:
        {
            NSLog(@"A gateway to the host server is working via WWAN.");
            self.hostActive = YES;

            break;

        }
    }

    [self commenceDownloadIfReachabilityIsOkay];
}

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

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

发布评论

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

评论(1

梦在深巷 2024-12-21 04:43:43

如果我正确理解您的代码,则希望调用:

[[self asiqueue]go ];

将导致不久前失败的 ASIHTTPRequest 重新排队并再次开始下载。这不起作用有两个原因:

  1. 一旦 ASIHTTPRequest 失败,它就会从队列中删除。
  2. 在已经调用过 go 的队列上调用 go 确实如此

解决方案是重新排队请求以重新启动它。

另外,http://allseeing-i.com/ASIHTTPRequest/How-to-use #resuming_interrupted_downloads 说:

另外,必须自己设置一个临时下载路径
(setTemporaryFileDownloadPath),带有部分数据的路径。

您的代码似乎没有这样做。 (您还需要 setDownloadDestinationPath

If I understand your code correctly, it is hoping that calling:

[[self asiqueue]go ];

will result in a ASIHTTPRequest that failed sometime ago getting requeued and starting download again. There's two reasons this won't work:

  1. Once an ASIHTTPRequest has failed, it's removed from the queue.
  2. Calling go on a queue that go has already been called on does
    nothing

The solution is to requeue the request to restart it.

Also, http://allseeing-i.com/ASIHTTPRequest/How-to-use#resuming_interrupted_downloads says:

Additionally, you must set a temporary download path yourself
(setTemporaryFileDownloadPath), with the path of the partial data.

which your code doesn't seem to be doing. (You also need to setDownloadDestinationPath )

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