Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 暂停并恢复网络队列_Ios_Asihttprequest_Nsoperationqueue - Fatal编程技术网

Ios 暂停并恢复网络队列

Ios 暂停并恢复网络队列,ios,asihttprequest,nsoperationqueue,Ios,Asihttprequest,Nsoperationqueue,我正在使用ASINetworkQueue在iPad应用程序中下载大约50个文件。我正在寻找一种允许用户暂停并恢复队列的方法 ASIHTTP文档参考 [request setAllowResumeForFileDownloads:YES]; 但这是在单个请求级别操作的,而不是在队列级别。因为ASINetworkQueue是NSOperationQueue的一个子类,所以我也尝试过 [queue setSuspended:YES]; 虽然这会暂停队列,但它不会影响正在进行的下载,它只是等待下载完

我正在使用ASINetworkQueue在iPad应用程序中下载大约50个文件。我正在寻找一种允许用户暂停并恢复队列的方法

ASIHTTP文档参考

[request setAllowResumeForFileDownloads:YES];
但这是在单个请求级别操作的,而不是在队列级别。因为ASINetworkQueue是NSOperationQueue的一个子类,所以我也尝试过

[queue setSuspended:YES];
虽然这会暂停队列,但它不会影响正在进行的下载,它只是等待下载完成,然后暂停队列,在我的情况下,这意味着用户按下按钮和队列实际暂停之间有很多秒,这不是我想要的UI体验

有人能提出另一种解决这个问题的方法吗?

我的解决方案

- (void) pause
{
    if(self.queue && self.queue.requestsCount>0)
    {
        NSLog(@"%@", self.queue.operations);

        for (ASIHTTPRequest * req in self.queue.operations) {
            req.userInfo = [NSDictionary dictionaryWithObject:@"1" forKey:@"ignore"];
        }

        [self.queue.operations makeObjectsPerformSelector:@selector(cancel)];

        [self.queue setSuspended:YES];

        for (ASIHTTPRequest * req in self.queue.operations) {
            ASIHTTPRequest * newReq = [[ASIHTTPRequest alloc] initWithURL:req.url];
            [newReq setDownloadDestinationPath:req.downloadDestinationPath];
            [newReq setTemporaryFileDownloadPath:req.temporaryFileDownloadPath];
//            [newReq setAllowResumeForFileDownloads:YES];
            [newReq setUserInfo:req.userInfo];
            [self.queue addOperation:newReq];

        }

    }
}

- (void) resume
{
    if(self.queue && self.queue.requestsCount>0)
    {
        [self _setupQueue];
        [self.queue go];
    }
}

- (void) _setupQueue
{
    [self.queue setShouldCancelAllRequestsOnFailure:NO];
    [self.queue setRequestDidStartSelector:@selector(downloadDidStart:)];
    [self.queue setRequestDidFinishSelector:@selector(downloadDidComplete:)];
    [self.queue setRequestDidFailSelector:@selector(downloadDidFailed:)];
    [self.queue setQueueDidFinishSelector:@selector(queueDidFinished:)];
    [self.queue setDownloadProgressDelegate:self.downloadProgress];
    [self.queue setDelegate:self];
    self.queue.maxConcurrentOperationCount = 3;
//    self.queue.showAccurateProgress = YES;
}
首先,暂停功能取消所有正在运行的操作,并重新创建新请求,将它们推入队列。 然后resume函数取消队列的挂起。 请注意,请求不应设置setAllowResumeForFileDownloads:YES,否则totalBytesToDownload将计算错误。如果allowResumeForFileDownloads=NO,其值将与队列中的请求计数相同

这是我的请求失败处理程序,我在文件下载失败时添加了重试。但是当我取消一个请求时,我不做什么,重试机制将被调用,所以我将userInfoignore:true设置为request对象以防止它发生

- (void) downloadDidFailed:(ASIHTTPRequest *)req
{
    NSLog(@"request failed");
    NSLog(@"%d", self.queue.requestsCount);

    if(![self.queue showAccurateProgress])
    {
        [self.queue setTotalBytesToDownload:[self.queue totalBytesToDownload]-1];
        NSLog(@"totalBytesToDownload=%lld", self.queue.totalBytesToDownload);
    }

    NSDictionary * userInfo = req.userInfo;
    if([[userInfo valueForKey:@"ignore"] boolValue]) return; // ignore retry

    int retryTimes = [[req.userInfo objectForKey:@"retryTimes"] intValue];
    if(retryTimes<kMU_MaxRetry)
    {
        ++ retryTimes;
        NSLog(@"retry %d", retryTimes);
        ASIHTTPRequest * newReq = [ASIHTTPRequest requestWithURL:req.url];
        [newReq setDownloadDestinationPath:req.downloadDestinationPath];
        [newReq setTemporaryFileDownloadPath:req.temporaryFileDownloadPath];
        newReq.userInfo = [NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"%d", retryTimes] forKey:@"retryTimes"];
        [self.queue addOperation:newReq];

        NSLog(@"%d", self.queue.requestsCount);
    }else{  // reach max retry, fail it
        [self.failures addObject:req];
    }
}
我不确定是否有更好的解决方案,希望它能帮助您