iOS:节流带宽,例如: Alamofire

时间:2016-09-29 12:32:36

标签: swift alamofire

是否可以限制上传操作的带宽,例如: Alamofire?

我想在用户使用应用程序的同时在后台上传数据,然后上传和下载更重要的内容。
因此,我想节省带宽特定情况下的背景。

我到目前为止找到的唯一可能是使用ASIHTTPRequest,它具有maxBandwidthPerSecond属性,但该库太旧了,我想使用更新的东西。

2 个答案:

答案 0 :(得分:0)

Chilkat API提供CKOSocket(),可以像这样限制使用的带宽:

//  To use bandwidth throttling, the connection should be made using the socket API.
//  This provides numerous properties to customize the connection, such as
//  BandwidthThrottleDown, BandwidthThrottleUp, ClientIpAddress, ClintPort, Http Proxy,
//  KeepAlive, PreferIpv6, RequireSslCertVerify, SoRcvBuf, SoSndBuf, SoReuseAddr,
//  SOCKS proxy, TcpNoSDelay, TlsPinSet, TlsCipherSuite, SslAllowedCiphers, etc.

let socket = CkoSocket()
var maxWaitMs: Int = 5000
var success: Bool = socket.Connect("content.dropboxapi.com", port: 443, ssl: true, maxWaitMs: maxWaitMs)
if success != true {
    print("\(socket.LastErrorText)")
    print("Connect Fail Reason: \(socket.ConnectFailReason.integerValue)")
    return
}

//  Set the upload bandwidth throttle rate to 50000 bytes per second.
socket.BandwidthThrottleUp = 50000 

Check this了解更多文档。

文档中的示例演示了如何使用REST API上传带宽限制。它会使用文件流将文件上传到Drobox,并限制可用于传输的带宽。

答案 1 :(得分:0)

我找不到任何关于 Alamofire (快速部分)的内容,但您可以使用 AFNetwork

从此link AFNetworking / AFNetworking / AFURLRequestSerialization.h )可以看到来源报告:

/**
 Throttles request bandwidth by limiting the packet size and adding a delay for each chunk read from the upload stream.
 When uploading over a 3G or EDGE connection, requests may fail with "request body stream exhausted". Setting a maximum packet size and delay according to the recommended values (`kAFUploadStream3GSuggestedPacketSize` and `kAFUploadStream3GSuggestedDelay`) lowers the risk of the input stream exceeding its allocated bandwidth. Unfortunately, there is no definite way to distinguish between a 3G, EDGE, or LTE connection over `NSURLConnection`. As such, it is not recommended that you throttle bandwidth based solely on network reachability. Instead, you should consider checking for the "request body stream exhausted" in a failure block, and then retrying the request with throttled bandwidth.
 @param numberOfBytes Maximum packet size, in number of bytes. The default packet size for an input stream is 16kb.
 @param delay Duration of delay each time a packet is read. By default, no delay is set.
 */
- (void)throttleBandwidthWithPacketSize:(NSUInteger)numberOfBytes
                                  delay:(NSTimeInterval)delay;

@end

您可以构建自定义" NetworkManager "在Objective-C中的类,可以轻松地将其导入到快速项目中。