【问题标题】:How can I resume ASINetworkQueue?如何恢复 ASINetworkQueue?
【发布时间】:2011-11-06 02:09:50
【问题描述】:

我正在尝试从与网络连接丢失时相同的位置恢复和排队。我的问题不在于可访问性,而在于恢复下载。我已经度过了整个周末,但没有运气。
非常感谢任何帮助。

下面的代码有方法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 ];

        }

    }

此方法只是不断轮询以检查网络状态 - 取自 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];
}

【问题讨论】:

    标签: objective-c ios4 asihttprequest nsoperationqueue


    【解决方案1】:

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

    [[self asiqueue]go ];
    

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

    1. 一旦 ASIHTTPRequest 失败,它就会从队列中删除。
    2. 在已调用 go 的队列上调用 go 什么都没有

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

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

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

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

    【讨论】:

    • 哇是的,这正是我所期望的,当然是错误的。我是否需要为 ios 设置setDownloadDestinationPath 。由于模拟器无法访问机器磁盘结构,可能的值是多少。
    • 是的,你需要为 iOS 设置一个有效的路径。 iOS 有一个完整的文件系统 - 根据文件是什么,您可能希望将其存储在应用程序下的文档目录中,代码类似于 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-31
    • 2021-03-01
    • 2021-07-21
    • 2010-10-12
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多