IOS: throttle width e.g. Alamofire

Is it possible to throttle the bandwidth of a boot operation, for example. Alamofire?

I would like to download data in the background when the user uses the application and downloads more important materials.
Therefore, I would like to limit the bandwidth in the background under certain circumstances.

The only opportunity I have found so far is to use ASIHTTPRequest , which has the maxBandwidthPerSecond property, but this library is too old, I would like to use something newer.

+6
source share
2 answers

The Chilkat API provides CKOSocket() , which provides the ability to throttle used bandwidths as follows:

 // 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 out for further documentation.

An example in the documentation demonstrates how to use boot bandwidth throttling using the REST API. It will upload the file to Drobox using a stream-limited file stream that can be used for transfer.

0
source

I can't find anything about Alamofire (swift part), but you can use AFNetwork .

As you can see from this (AFNetworking / AFNetworking / AFURLRequestSerialization.h) sources report:

 /** 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 

You can create your own NetworkManager class in objective-C and easily import it into your quick project.

0
source

All Articles