Skip to content

Commit 7c141b2

Browse files
Fix Swift Concurrency Issues and Compiler Warnings (#214)
1 parent 3eb006f commit 7c141b2

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

DebugSwift/Sources/Features/Network/Helpers/WKWebViewNetworkMonitor.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -774,24 +774,72 @@ final class WebViewNetworkMessageHandler: NSObject, WKScriptMessageHandler {
774774

775775
// MARK: - Request Cache
776776

777+
struct WebViewRequestInfo: Sendable {
778+
let requestId: String
779+
let url: String
780+
let method: String
781+
let startTime: Date
782+
let headers: [String: String]
783+
let body: String?
784+
785+
init(from dictionary: [String: Any]) {
786+
self.requestId = dictionary["requestId"] as? String ?? ""
787+
self.url = dictionary["url"] as? String ?? ""
788+
self.method = dictionary["method"] as? String ?? "GET"
789+
self.startTime = dictionary["startTime"] as? Date ?? Date()
790+
self.headers = dictionary["headers"] as? [String: String] ?? [:]
791+
792+
// Convert body to string if needed
793+
if let bodyData = dictionary["body"] {
794+
if let bodyString = bodyData as? String {
795+
self.body = bodyString
796+
} else if let bodyDict = bodyData as? [String: Any],
797+
let data = try? JSONSerialization.data(withJSONObject: bodyDict),
798+
let jsonString = String(data: data, encoding: .utf8) {
799+
self.body = jsonString
800+
} else {
801+
self.body = "\(bodyData)"
802+
}
803+
} else {
804+
self.body = nil
805+
}
806+
}
807+
808+
func toDictionary() -> [String: Any] {
809+
var dict: [String: Any] = [
810+
"requestId": requestId,
811+
"url": url,
812+
"method": method,
813+
"startTime": startTime,
814+
"headers": headers
815+
]
816+
817+
if let body = body {
818+
dict["body"] = body
819+
}
820+
821+
return dict
822+
}
823+
}
824+
777825
final class WebViewRequestCache: @unchecked Sendable {
778826
static let shared = WebViewRequestCache()
779827

780-
private var cache: [String: [String: Any]] = [:]
828+
private var cache: [String: WebViewRequestInfo] = [:]
781829
private let queue = DispatchQueue(label: "com.debugswift.webview.cache", attributes: .concurrent)
782830

783831
private init() {}
784832

785833
nonisolated func store(requestId: String, requestInfo: [String: Any]) {
786-
queue.async(flags: .barrier) { [weak self, requestInfo] in
787-
// Safe: We control the threading and dictionary access via barriers
788-
self?.cache[requestId] = requestInfo
834+
let sendableInfo = WebViewRequestInfo(from: requestInfo)
835+
queue.async(flags: .barrier) { [weak self] in
836+
self?.cache[requestId] = sendableInfo
789837
}
790838
}
791839

792840
nonisolated func retrieve(requestId: String) -> [String: Any]? {
793841
return queue.sync { [weak self] in
794-
return self?.cache[requestId]
842+
return self?.cache[requestId]?.toDictionary()
795843
}
796844
}
797845

DebugSwift/Sources/Features/Network/Main/Network.ViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,13 @@ final class NetworkViewModel {
7373
let webViewRequests = webViewModels
7474
HttpDatasource.shared.removeAll()
7575
// Re-add WebView requests
76-
webViewRequests.forEach { HttpDatasource.shared.addHttpRequest($0) }
76+
webViewRequests.forEach { _ = HttpDatasource.shared.addHttpRequest($0) }
7777
case .webview:
7878
// Remove only WebView requests
7979
let httpRequests = httpModels
8080
HttpDatasource.shared.removeAll()
8181
// Re-add HTTP requests
82-
httpRequests.forEach { HttpDatasource.shared.addHttpRequest($0) }
82+
httpRequests.forEach { _ = HttpDatasource.shared.addHttpRequest($0) }
8383
case .websocket:
8484
// WebSocket clearing handled elsewhere
8585
break

0 commit comments

Comments
 (0)