1

我试图用来AFNetworking粗略估计上传带宽。我有一个NSObject观察AFNetworkingOperationDidStartNotificationand的子类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 字节;我在数学中遗漏了什么吗?

我计划使用类似的方法制作更复杂的带宽检测器,但现在希望在每次上传的基础上获得准确的上传带宽。

4

0 回答 0