@@ -11,11 +11,12 @@ import Foundation
11
11
protocol MQTTSessionStreamDelegate : class {
12
12
func mqttReady( _ ready: Bool , in stream: MQTTSessionStream )
13
13
func mqttErrorOccurred( in stream: MQTTSessionStream , error: Error ? )
14
- func mqttReceived( in stream: MQTTSessionStream , _ read: StreamReader )
14
+ func mqttReceived( in stream: MQTTSessionStream , _ read: StreamReader )
15
15
}
16
16
17
17
class MQTTSessionStream : NSObject {
18
18
19
+ private var currentRunLoop : RunLoop !
19
20
private let inputStream : InputStream ?
20
21
private let outputStream : OutputStream ?
21
22
private var sessionQueue : DispatchQueue
@@ -39,11 +40,17 @@ class MQTTSessionStream: NSObject {
39
40
40
41
inputStream? . delegate = self
41
42
outputStream? . delegate = self
42
-
43
+
43
44
sessionQueue. async { [ weak self] in
44
- let currentRunLoop = RunLoop . current
45
- inputStream? . schedule ( in: currentRunLoop, forMode: . defaultRunLoopMode)
46
- outputStream? . schedule ( in: currentRunLoop, forMode: . defaultRunLoopMode)
45
+
46
+ guard let `self` = self else {
47
+ return
48
+ }
49
+
50
+ self . currentRunLoop = RunLoop . current
51
+ inputStream? . schedule ( in: self . currentRunLoop, forMode: . defaultRunLoopMode)
52
+ outputStream? . schedule ( in: self . currentRunLoop, forMode: . defaultRunLoopMode)
53
+
47
54
inputStream? . open ( )
48
55
outputStream? . open ( )
49
56
if ssl {
@@ -53,19 +60,19 @@ class MQTTSessionStream: NSObject {
53
60
}
54
61
if timeout > 0 {
55
62
DispatchQueue . global ( ) . asyncAfter ( deadline: . now( ) + timeout) {
56
- self ? . connectTimeout ( )
63
+ self . connectTimeout ( )
57
64
}
58
65
}
59
- currentRunLoop. run ( )
66
+ self . currentRunLoop. run ( )
60
67
}
61
68
}
62
69
63
70
deinit {
64
71
delegate = nil
65
72
inputStream? . close ( )
66
- inputStream? . remove ( from: . current , forMode: . defaultRunLoopMode)
73
+ inputStream? . remove ( from: currentRunLoop , forMode: . defaultRunLoopMode)
67
74
outputStream? . close ( )
68
- outputStream? . remove ( from: . current , forMode: . defaultRunLoopMode)
75
+ outputStream? . remove ( from: currentRunLoop , forMode: . defaultRunLoopMode)
69
76
}
70
77
71
78
var write : StreamWriter ? {
0 commit comments