Instance Method

readMinimumLength(_:maximumLength:completionHandler:)

Read the requested range of bytes

Declaration

func readMinimumLength(_ minimum: Int, maximumLength maximum: Int, completionHandler completion: @escaping (Data?, Error?) -> Void)

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 be nil, 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, and data 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.

See Also

Transferring Data

func readLength(Int, completionHandler: (Data?, Error?) -> Void)

Read a certain number of bytes on a connection

func writeClose()

Close the connection for writing

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software