Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>My solution..</p> <pre><code>- (void) pause { if(self.queue &amp;&amp; self.queue.requestsCount&gt;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 &amp;&amp; self.queue.requestsCount&gt;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; } </code></pre> <p>First, pause function cancel all running operations, and recreate new requests push them into queue. Then resume function unsuspends the queue. Be noticed, request should not set setAllowResumeForFileDownloads:YES, otherwise the totalBytesToDownload will calculate wrong..If allowResumeForFileDownloads=NO, its value will be same as the count of requests in queue.</p> <p>Here is my request fail handler, I add retry when file download fail. But I don't whant when I cancel a request, the retry mechanism will be invoked, so I set userInfo(ignore:true) to request object to prevent it happens.</p> <pre><code>- (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&lt;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]; } } </code></pre> <p>I am not sure if there is a better solution, hope it can help you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload