iOS AFNetworking downloadProgressBlock计算返回负数

iOS AFNetworking downloadProgressBlock计算返回负数,第1张

概述我正在使用AFNetworking下载使用第三方类解析的数据.我曾经多次使用AFNetworking做类似的动作,但出于某种原因,当我调用downloadProgressBlock并进行计算以使用我的进度条时,数字会返回负数.见下文: 2013-09-22 16:04:06.036 Portland Police[2228:60b] Download = -31849.0000002013-09 我正在使用AFNetworking下载使用第三方类解析的数据.我曾经多次使用AFNetworking做类似的动作,但出于某种原因,当我调用downloadProgressBlock并进行计算以使用我的进度条时,数字会返回负数.见下文:

2013-09-22 16:04:06.036 Portland Police[2228:60b] Download = -31849.0000002013-09-22 16:04:06.041 Portland Police[2228:60b] Download = -40537.0000002013-09-22 16:04:06.042 Portland Police[2228:60b] Download = -44881.0000002013-09-22 16:04:06.044 Portland Police[2228:60b] Download = -53569.0000002013-09-22 16:04:06.046 Portland Police[2228:60b] Download = -62257.0000002013-09-22 16:04:06.048 Portland Police[2228:60b] Download = -63705.0000002013-09-22 16:04:06.085 Portland Police[2228:60b] Download = -70945.0000002013-09-22 16:04:06.087 Portland Police[2228:60b] Download = -89769.0000002013-09-22 16:04:06.089 Portland Police[2228:60b] Download = -94113.0000002013-09-22 16:04:06.100 Portland Police[2228:60b] Download = -98457.0000002013-09-22 16:04:06.104 Portland Police[2228:60b] Download = -102801.0000002013-09-22 16:04:06.111 Portland Police[2228:60b] Download = 1.000000

以下是我的代码:

// Get the URL we are going to use to parse withAFhttpClIEnt *httpClIEnt = [[AFhttpClIEnt alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.portlandonline.com/scripts/911incIDents.cfm"]];NSURLRequest *request = [httpClIEnt requestWithMethod:@"GET" path:nil parameters:nil];AFhttpRequestoperation *operation = [httpClIEnt httpRequestoperationWithRequest:request success:^(AFhttpRequestoperation *operation,ID responSEObject) {    NSLog(@"Response = %@",operation);    // Empty our calls out    [self.originalArray removeAllObjects];    // Initiate our parser    MWFeedParser *parser = [[MWFeedParser alloc] init];    parser.delegate = self;    [parser startParsingData:responSEObject textEnCodingname:[operation.response textEnCodingname] withCompletionHandler:^(NSError *error,MWFeedParser *parser) {        // If theres no error        if (!error) {            // Return the success block            success(operation.request,operation.request.URL,self.calls);        }        else {            // Return the failure block            failure(operation.request,error);        }    }];} failure:^(AFhttpRequestoperation *operation,NSError *error) {    NSLog(@"AFhttpRequestoperation Failure: %@",error);    // Return our failure block    failure(operation.request,error);}];[operation setDownloadProgressBlock:^(NSUInteger bytesRead,long long totalBytesRead,long long totalBytesExpectedToRead) {    NSLog(@"Download = %f",(float)totalBytesRead / totalBytesExpectedToRead);}];[operation start];
解决方法 我怀疑你会发现问题是totalBytesExpectedToRead.具体来说,这来自NSURLResponse属性expectedContentLength,其中 as the docs say:

Return Value

The receiver’s expected content length,or NSURLResponseUnkNownLength if the length can’t be determined.

discussion

Some protocol implementations report the content length as part of the response,but not all protocols guarantee to deliver that amount of data. ClIEnts should be prepared to deal with more or less data.

顺便说一下,这个常量NSURLResponseUnkNownLength等于-1,这可以解释你的计算值.这只是系统通知您无法确定响应长度的方式.

因此,在最坏的情况下,您将获得-1作为预期的字节数.即使它不是-1,它也不完全可靠(特别是如果你编写了自己的服务器代码就会出现问题).您的应用必须正常处理向您的应用报告的所有expectedContentLength.大多数情况下,如果你得到一个非负值,你通常得到的值可以有效地用于“完成百分比”计算,但不要依赖它(例如,你可能会收到更多字节或者比expectedContentLength报告更少的字节).优雅地处理特殊的价值观.

例如,在设置我的进度条时,我做了类似的事情:

CGfloat progress;if (expectedContentLength > 0 && progressContentLength <= expectedContentLength)     progress = (CGfloat) progressContentLength / expectedContentLength;else     progress = (progressContentLength % 1000000l) / 1000000.0f;

这给了我一个进度条,如果我有一个很好的预期内容长度值,随着下载的进展缓慢进展到100%,但另外显示我进度条从下载的每1,000,000字节的0%进展到100%.后一种情况并不理想,但除非您的服务器报告准确的预期内容长度,否则您无法做很多事情.

或者,如果您从未获得可靠的expectedContentLength,则可以选择使用不确定的进度视图(例如UIActivityIndi​​catorVIEw),这样可以完全避免此问题.

最重要的是,成功的进度计算取决于您的服务器是否为预期的字节提供合法值.如果你看看totalBytesExpectedToRead,我敢打赌你得到-1,结果看到你好奇的计算进度百分比值.

总结

以上是内存溢出为你收集整理的iOS AFNetworking downloadProgressBlock计算返回负数全部内容,希望文章能够帮你解决iOS AFNetworking downloadProgressBlock计算返回负数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1018501.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存