When writing to file using OutputStream, after that stream has been closed, there is no error, it silently fails to write.
Starting:
self.outputStream = OutputStream(toFileAtPath: filePath, append: true)
self.outputStream.delegate = self
self.outputStream.schedule(in: RunLoop.current, forMode: RunLoopMode.defaultRunLoopMode)
self.open()
Delegate is working:
func stream(_ aStream: Stream, handle eventCode: Stream.Event) {
//Will report that stream was opened.
... Writing some text - successful ...
self.outputStream.close()
self.outputStream.write(newData, maxLength: newData.count)
And.. nothing. No text is written, yet no error is reported.
Does writing to a closed stream not triggering an error? That is, should we check the stream status before writing?
You should always check the return value from
write(), which is the number of bytes written to the stream, or-1on error:Writing to the output stream after it has been closed is an error, and will be detected by that check.
The stream's
streamErrorcan provide additional information in the case of an error, however it isnilin this particular situation.