- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有以下问题,这让我抓狂了几个星期。
我有一个用于下载文件的小型框架。该框架具有暂停和恢复文件下载的能力。到目前为止,一切都很好。问题是,每次我暂停下载然后恢复下载后,负责下载的 NSURLConnection
不会调用 connectionDidFinishLoading
,如果下载的字节等于预期的文件大小,但保持在调用 connectionDidReceiveData
时破坏了我的下载。我不知道为什么会这样。当我不暂停/恢复下载时,一切正常。下面是暂停和恢复下载方法的代码。
- (id)pause
{
[self.connection cancel];
self.connection = nil;
return self;
}
- (id)resume
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:self.url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:600];
NSFileManager *manager = [NSFileManager defaultManager];
NSString *localSavePath = self.savePath;
if (failBlocks.count > 0) {
[self cancel];
[self start];
}
else {
if( ![manager fileExistsAtPath:localSavePath] )
{
[manager createFileAtPath:localSavePath contents:[NSData data] attributes:nil];
}
if (self.downloadData.length > 0) {
log_muma2(@"Should resume url %@",self.url);
// Define the bytes we wish to download.
NSString *range = [NSString stringWithFormat:@"bytes=%i-", downloadData.length];
[request setValue:range forHTTPHeaderField:@"Range"];
}
if (!self.connection) {
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
self.connection = conn;
}
}
return self;
}
如果有人能帮我解决这个问题,我会很高兴。
我已经测试过,如果已经下载的数据大小正确等等。一切似乎都很好。
非常感谢。特立独行
=========编辑=========
这是我的didReceiveData
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
amountDownloaded += data.length;
NSInteger receivedLen = [data length];
bytesReceived = (bytesReceived + receivedLen);
if(expectedSize != NSURLResponseUnknownLength) {
progress = ((bytesReceived/(float)expectedSize)*100)/100;
[self performSelectorOnMainThread:@selector(updateProgressBar) withObject:nil waitUntilDone:NO];
percentComplete = progress*100;
}
if (self.savePath == nil || [self.savePath isEqualToString:@""]) {
[self.downloadData appendData:data];
}
else {
[self.downloadData appendData:data];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:self.savePath];
[handle seekToEndOfFile];
[handle writeData:data];
//
}
if (expectedSize < bytesReceived)
{
NSLog(@"download exceeded expected size %f with %lld", expectedSize, bytesReceived);
[self pause];
[self cancel];
self.connection = nil;
self.downloadData = nil;
bytesReceived = 0;
expectedSize = 0;
amountDownloaded = 0;
progress = 0;
percentComplete = 0;
[self start];
}
}
最佳答案
您如何计算预期的文件大小?
如果您使用 response.expectedContentLength
,请注意每次您在恢复下载时初始化新连接时该值都会减少。
关于iphone - 下载文件时,NSURLConnection 不会在文件末尾调用 didFinishLoading,当下载暂停并恢复时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11556820/
我在应用程序中有一个登录屏幕 View 和 5 个选项卡。我希望当我完成登录屏幕时,它应该移动到选项卡 View (5)。为此,一旦登录任务完成,我必须删除 View 并添加选项卡的另一个 View
我有一些代码需要在 UIWebView 完成加载文档后运行。为此,我将 UIWebView 的委托(delegate)设置为我的 Controller ,并实现了 webViewDidFinishLo
我正在连接到一个网站,登录并使用 NSURLRequest 获取页面的 HTML 源代码,这一切正常。我通过将它打印到控制台来确认 html 源是我需要的。问题是我需要在 javascript 代码完
我有一个工作正常的UIWebView。代表正在工作。当我打开一个 URL 时,事件 shouldStartLoadWithRequest 和 webViewDidFinishLoad 被触发。 但是当
我有以下问题,这让我抓狂了几个星期。 我有一个用于下载文件的小型框架。该框架具有暂停和恢复文件下载的能力。到目前为止,一切都很好。问题是,每次我暂停下载然后恢复下载后,负责下载的 NSURLConne
我绝对无法调整我的 UIWebView 的大小。 我的 UIWebView 在 .h 中声明并在 IB 中连接。 IB 中的高度是 110。但我的文本高度约为 1500。现在我想更改高度,以便无需滚动
我在 Windows 上使用 WebKit 的 Chromium 端口,我正在尝试检索文档中所有图像的列表。我认为最好的方法是像这样实现 WebKit::WebFrameClient::didFini
我是一名优秀的程序员,十分优秀!