@@ -36,11 +36,12 @@ class TikOperation: Operation {
36
36
self . tikUseCase = tikUseCase
37
37
self . path = path ?? " "
38
38
self . contentType = contentType ?? ContentType . JSON
39
+ super. init ( )
39
40
}
40
41
41
42
private var timer : Timer ?
42
43
43
- private var runing : Bool = false {
44
+ private var running : Bool = false {
44
45
didSet {
45
46
willChangeValue ( forKey: " isFinished " )
46
47
willChangeValue ( forKey: " isExecuting " )
@@ -49,39 +50,46 @@ class TikOperation: Operation {
49
50
}
50
51
}
51
52
52
- override var isAsynchronous : Bool { true }
53
+ override var isAsynchronous : Bool { true }
53
54
54
- override var isFinished : Bool { !runing }
55
+ override var isFinished : Bool { !running }
55
56
56
- override var isExecuting : Bool { runing }
57
+ override var isExecuting : Bool { running }
57
58
58
59
override func start( ) {
59
- guard !isCancelled else { return }
60
- runing = true
61
-
62
- self . timer = Timer ( fire: self . dispatchDate, interval: 0 , repeats: false , block: { [ self ] ( timer) in
63
- let track = { [ self ] ( data: Encodable ? ) in
60
+ guard !isCancelled else { return }
61
+ running = true
62
+
63
+ self . timer = Timer ( fire: self . dispatchDate, interval: 0 , repeats: false , block: { [ weak self] ( timer) in
64
+ guard let self = self else { return }
65
+
66
+ let track = { [ weak self] ( data: Encodable ? ) in
67
+ guard let self = self else { return }
68
+
64
69
let params = data? . params
65
-
70
+
66
71
if let params = params {
67
- tikUseCase. tik ( path: path, type: contentType, params: params)
72
+ self . tikUseCase. tik ( path: self . path, type: self . contentType, params: params)
68
73
}
69
- timer. invalidate ( )
70
- runing = false
74
+ self . finish ( )
71
75
}
72
- let data = dataBuilder ( track)
76
+
77
+ let data = self . dataBuilder ( track)
73
78
74
79
if data != nil {
75
80
track ( data)
76
81
}
77
82
} )
78
83
79
- RunLoop . current. add ( self . timer!, forMode: . common)
80
- RunLoop . current. run ( )
84
+ RunLoop . main. add ( self . timer!, forMode: . common)
81
85
}
82
86
83
87
override func cancel( ) {
84
88
timer? . invalidate ( )
85
- runing = false
89
+ finish ( )
90
+ }
91
+
92
+ private func finish( ) {
93
+ running = false
86
94
}
87
95
}
0 commit comments