Skip to content

Commit c4a5bff

Browse files
authored
Merge pull request #87 from wangyuchaogeek/main
fix:并发日志崩溃
2 parents 7a963ac + e0c2edd commit c4a5bff

2 files changed

Lines changed: 28 additions & 3 deletions

File tree

Example/Pods/Pods.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/SmartCodable/Core/Sentinel/SmartSentinel.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public struct SmartSentinel {
2020

2121
/// 设置回调方法,传递解析完成时的日志记录
2222
public static func onLogGenerated(handler: @escaping (String) -> Void) {
23-
self.logsHandler = handler
23+
handlerQueue.sync {
24+
self.logsHandler = handler
25+
}
2426
}
2527

2628
/// Set up different levels of padding
@@ -45,6 +47,10 @@ public struct SmartSentinel {
4547

4648
/// 回调闭包,用于在解析完成时传递日志
4749
private static var logsHandler: ((String) -> Void)?
50+
51+
/// 用于同步访问 logsHandler 的队列
52+
private static let handlerQueue = DispatchQueue(label: "com.smartcodable.handler", qos: .utility)
53+
4854
}
4955

5056

@@ -107,7 +113,13 @@ extension SmartSentinel {
107113
message += getFooter()
108114
print(message)
109115

110-
logsHandler?(message)
116+
handlerQueue.sync {
117+
if let handler = logsHandler {
118+
DispatchQueue.main.async {
119+
handler(message)
120+
}
121+
}
122+
}
111123
}
112124

113125
cache.clearCache(parsingMark: parsingMark)
@@ -131,7 +143,13 @@ extension SmartSentinel {
131143
message += getFooter()
132144
print(message)
133145

134-
logsHandler?(message)
146+
handlerQueue.sync {
147+
if let handler = logsHandler {
148+
DispatchQueue.main.async {
149+
handler(message)
150+
}
151+
}
152+
}
135153
}
136154
}
137155
}

0 commit comments

Comments
 (0)