Read the requested range of bytes
SDKs
- iOS 9.0+
- macOS 10.11+
- Mac Catalyst 13.0+
Framework
- Network
Extension
Declaration
Parameters
minimumLength
The minimum number of bytes the caller wants to read.
maximumLength
The maximum number of bytes the caller wants to read.
completionHandler
The completion handler to be invoked when data has been read or an error occurred.
Discussion
The completion handler will be invoked when:
-
The exact number of requested bytes have been read;
data
will be non-nil
. -
Fewer than the requested number of bytes, or no bytes, have been read, and the connection's read side has been closed.
data
might benil
, depending on whether there was any data to be read when the connection's read side was closed. -
Some fatal error has occurred, returned in
error
, anddata
will be nil.
To know when to schedule a read again, check for the condition whether an error has occurred.
For better performance, the caller should pick the effective minimum and maximum lengths. For example, if the caller absolutely needs a specific number of bytes before it can make any progress, use that value as the minimum. The maximum bytes can be the upperbound that the caller wants to read. Typically, the minimum length can be the caller protocol fixed-size header and the maximum length can be the maximum size of the payload or the size of the current read buffer.