我正在尝试使用AFNetworking
粗略估算上传带宽。我有NSObject
的子类来观察AFNetworkingOperationDidStartNotification
和AFNetworkingOperationDidFinishNotification
。我得到了在AFHTTPRequestOperation
进度块中写入的总预期字节数:
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
if (lastUploadSize != totalBytesExpectedToWrite)
lastUploadSize = totalBytesExpectedToWrite;
}];
在开始和结束时,我将当前日期保存到各自的iVar中。然后我调用一个方法,calculateBandwidth
进行一些简单的计算:
-(void)calculateBandwidth {
NSTimeInterval difference = [uploadFinishTime timeIntervalSinceDate:uploadStartTime];
float bandwidth = (lastUploadSize * 0.0078125) / difference;
PLog(@"Bandwidth (calculated): %f kbps", bandwidth);
if ([_delegate respondsToSelector:@selector(networkClientDidUpdateBandwidth:time:withSize:)])
[_delegate networkClientDidUpdateBandwidth:bandwidth time:difference withSize:lastUploadSize];
}
但带宽结果似乎已经消失。 0.0078125千比特= 1字节,如果我没有记错的话;我在数学中遗漏了什么吗?
我计划使用类似的方法制作更复杂的带宽检测器,但现在希望在每次上传的基础上获得准确的上传带宽。