1

The following code used to work perfectly in Xcode 15. (Release mode)

I was able to receive non-zero data from the stream

self.inputStream?.read(&inputbuffer, maxLength: readsize)

However, after switching to Xcode 16, stream always returns 0. The code hasn't changed at all.

I’ve already tried several things to troubleshoot this:

  • Changed the scheme settings

  • Verified that Info.plist contains proper App Transport Security (ATS) settings, including:

      NSExceptionDomains for my host
    
      NSAllowsArbitraryLoads set to true
    

But none of these made a difference.

private let host = "my host"
private let port = 443
private var inputStream: InputStream?
private var outputStream: OutputStream?

open func connect() {
    
    Stream.getStreamsToHost(withName: host, port: port, inputStream: &self.inputStream, outputStream: &self.outputStream)
    
    if let inputStream = inputStream, let outputStream = outputStream {
        inputStream.delegate = self
        outputStream.delegate = self
        
        CFReadStreamSetDispatchQueue(inputStream, PMMStreamQueue)
        CFWriteStreamSetDispatchQueue(outputStream, PMMStreamQueue)
         
        inputStream.open()
        outputStream.open()
    }
}

func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
    switch eventCode {
    case Stream.Event.endEncountered:
        print("endEncountered")
    case Stream.Event.hasBytesAvailable:
        let readsize = 8192 * 4
        var inputbuffer: [UInt8] = [UInt8](repeating: 0, count: readsize)
        let readBytesCount = self.inputStream?.read(&inputbuffer, maxLength: readsize) ?? 0
        print(readBytesCount)
    default:
        break
    }
}

I'm currently using the Release scheme, and the issue only seems to occur in Release mode under Xcode 16.

Does anyone know how to fix this or what's causing the issue?

Thanks in advance!

1
  • 1
    getStreamsToHost is deprecated, may be that is the reason. Commented Apr 23 at 7:41

1 Answer 1

0

✅ SOLVED!

The issue was caused by Swift compiler optimizations.

In Build Settings, under Swift Compiler - Code Generation → Optimization Level,
select No Optimization [-Onone] instead of any optimized level (like -O or -Osize).

It seems that enabling optimization affects the behavior of the Stream APIs.
Once optimizations are disabled, everything works correctly again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.