NSURLSession, streamTaskWithHostName with streaming data

I am trying to use a URLSessionStreamTask in Swift for data streaming at a rate of many megabits (but likely being sent in network packets of only kbytes).

What should I set maxBytes to be in:


myStreamTask.readData(ofMinLength: minBytes,   /
            maxLength: maxBytes,
            timeout: timeout, completionHandler: { (data: Data?, flag: Bool, err: Error?) -> Void in
     if data != nil { /* data handler */ } }


since I expect an unlimited number of bytes (until the user stops or quits the iOS app tomorrow, etc.). (The rest of the Swift 3 source code for this class is in a github gist under the name hotpaw2.)


Since each data block will contain less than an infinite amount, where do I keep calling readData again to keep receiving more data as it is being streamed?

Inside the data handler block? Or somewhere else?


What happens if readData() is not being called often enough or with a large enough maxLength for the stream rate? Will readData() get an error?


Currently, receiving this data stream (from an RTL-SDR rtl_tcp socket) works just fine using the older NSInputStream API.

What are the advantages (if any) of getting this continuous download to work using an NSURLSession streamTask instead?

Replies

Currently, receiving this data stream … works just fine using the older NSInputStream API. What are the advantages (if any) of getting this continuous download to work using an NSURLSession streamTask instead?

If you have working code based on NSStream I wouldn’t bother rewriting it to use NSURLSession stream tasks unless that API offers some specific feature that you really need. Does it?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"