Skip to content

Commit

Permalink
Bug/nil run loop (aciidgh#41)
Browse files Browse the repository at this point in the history
* currentRunLoop no long IUO

* Ensure currentRunLoop is not nil before trying to remove it from streams
  • Loading branch information
Michael-AirMap authored and adolfo committed May 11, 2019
1 parent e412d41 commit 6667e19
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions SwiftMQTT/SwiftMQTT/MQTTSessionStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protocol MQTTSessionStreamDelegate: class {

class MQTTSessionStream: NSObject {

private var currentRunLoop: RunLoop!
private var currentRunLoop: RunLoop?
private let inputStream: InputStream?
private let outputStream: OutputStream?
private var sessionQueue: DispatchQueue
Expand Down Expand Up @@ -48,8 +48,8 @@ class MQTTSessionStream: NSObject {
}

self.currentRunLoop = RunLoop.current
inputStream?.schedule(in: self.currentRunLoop, forMode: .defaultRunLoopMode)
outputStream?.schedule(in: self.currentRunLoop, forMode: .defaultRunLoopMode)
inputStream?.schedule(in: self.currentRunLoop!, forMode: .defaultRunLoopMode)
outputStream?.schedule(in: self.currentRunLoop!, forMode: .defaultRunLoopMode)

inputStream?.open()
outputStream?.open()
Expand All @@ -63,12 +63,13 @@ class MQTTSessionStream: NSObject {
self.connectTimeout()
}
}
self.currentRunLoop.run()
self.currentRunLoop!.run()
}
}

deinit {
delegate = nil
guard let currentRunLoop = currentRunLoop else { return }
inputStream?.close()
inputStream?.remove(from: currentRunLoop, forMode: .defaultRunLoopMode)
outputStream?.close()
Expand Down

0 comments on commit 6667e19

Please sign in to comment.