Skip to content

Commit f96d465

Browse files
authored
fix: dead threads (#21)
1 parent 6fff8c8 commit f96d465

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

CompassSDK/Tick/TikOperation.swift

+25-17
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@ class TikOperation: Operation {
3636
self.tikUseCase = tikUseCase
3737
self.path = path ?? ""
3838
self.contentType = contentType ?? ContentType.JSON
39+
super.init()
3940
}
4041

4142
private var timer: Timer?
4243

43-
private var runing: Bool = false {
44+
private var running: Bool = false {
4445
didSet {
4546
willChangeValue(forKey: "isFinished")
4647
willChangeValue(forKey: "isExecuting")
@@ -49,39 +50,46 @@ class TikOperation: Operation {
4950
}
5051
}
5152

52-
override var isAsynchronous: Bool {true}
53+
override var isAsynchronous: Bool { true }
5354

54-
override var isFinished: Bool {!runing}
55+
override var isFinished: Bool { !running }
5556

56-
override var isExecuting: Bool {runing}
57+
override var isExecuting: Bool { running }
5758

5859
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+
6469
let params = data?.params
65-
70+
6671
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)
6873
}
69-
timer.invalidate()
70-
runing = false
74+
self.finish()
7175
}
72-
let data = dataBuilder(track)
76+
77+
let data = self.dataBuilder(track)
7378

7479
if data != nil {
7580
track(data)
7681
}
7782
})
7883

79-
RunLoop.current.add(self.timer!, forMode: .common)
80-
RunLoop.current.run()
84+
RunLoop.main.add(self.timer!, forMode: .common)
8185
}
8286

8387
override func cancel() {
8488
timer?.invalidate()
85-
runing = false
89+
finish()
90+
}
91+
92+
private func finish() {
93+
running = false
8694
}
8795
}

MarfeelSDK-iOS.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
1616
#
1717

1818
spec.name = "MarfeelSDK-iOS"
19-
spec.version = "2.10.0"
19+
spec.version = "2.10.1"
2020
spec.summary = "iOS version of MarfeelSDK."
2121

2222
# This description is used to generate tags and improve search results.

0 commit comments

Comments
 (0)