关于使用ASIHTTPRequest进行断点续传

最近项目中用到了ASI的断点续传,把其中遇到的问题和解决方法与大家分享

NSString *bookPath = [RBUtilsObject filePathString:s9id];
    ASIHTTPRequest *bookDownloadRequest = [[ASIHTTPRequest alloc]initWithURL:requestURL];
    bookDownloadRequest.shouldContinueWhenAppEntersBackground = YES;
    [bookDownloadRequest setDownloadDestinationPath:bookPath];
    NSString *tmpbookPath = [RBUtilsObject tmpfilePathString:s9id];
    [bookDownloadRequest setTemporaryFileDownloadPath:tmpbookPath];
    [bookDownloadRequest setAllowResumeForFileDownloads:YES];
    bookDownloadRequest.allowCompressedResponse = NO;
       
    __block NSString *currentDownloadBookID = s9id;
    __block BOOL isAddToShelf = NO;

    [bookDownloadRequest setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
        if ( total !=0) {
            if (isAddToShelf == NO) {
                isAddToShelf = YES;
                [[NSNotificationCenter defaultCenter]postNotificationName:@(kIsAddToShelf) object:nil userInfo:[NSDictionary dictionaryWithObjectsAndKeys:currentDownloadBookID,@(kCurrentDownloadBookID), nil]];//s9id
            }
            float progressAmount = (float)((size*1.0)/(total*1.0));           
            NSLog(@"downloadPercent=%f  s9id= %@",progressAmount,currentDownloadBookID);
            //这里的size其实是request接收到的数据的回调block,并不是当前全部已下载的数据,因此在这样不能正确显示下载进度
           [[NSNotificationCenter defaultCenter]postNotificationName:currentDownloadBookID object:@(progressAmount)];
            
            
        }
    }];


解决方案如下:找到ASIHTTPRequest的.m文件

- (void)updateDownloadProgress
{
	// We won't update download progress until we've examined the headers, since we might need to authenticate
	if (![self responseHeaders] || [self needsRedirect] || !([self contentLength] || [self complete])) {
		return;
	}
		
	unsigned long long bytesReadSoFar = [self totalBytesRead]+[self partialDownloadSize];
	unsigned long long value = 0;
	
	if ([self showAccurateProgress] && [self contentLength]) {
		value = bytesReadSoFar-[self lastBytesRead];
		if (value == 0) {
			return;
		}
	} else {
		value = 1;
		[self setUpdatedProgress:YES];
	}
	if (!value) {
		return;
	}

	[ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&queue withObject:self amount:&value callerToRetain:self];
	[ASIHTTPRequest performSelector:@selector(request:didReceiveBytes:) onTarget:&downloadProgressDelegate withObject:self amount:&value callerToRetain:self];

	[ASIHTTPRequest updateProgressIndicator:&downloadProgressDelegate withProgress:[self totalBytesRead]+[self partialDownloadSize] ofTotal:[self contentLength]+[self partialDownloadSize]];

	#if NS_BLOCKS_AVAILABLE
    if (bytesReceivedBlock) {
		unsigned long long totalSize = [self contentLength] + [self partialDownloadSize];
//		[self performBlockOnMainThread:^{ if (bytesReceivedBlock) { bytesReceivedBlock(value, totalSize); }}];此处修改为即可将下载进度正确返回
        [self performBlockOnMainThread:^{
            if (bytesReceivedBlock) {
                bytesReceivedBlock(([self totalBytesRead] + [self partialDownloadSize]),totalSize);
            }
        }];
    }
	#endif
	[self setLastBytesRead:bytesReadSoFar];
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值