@@ -19,6 +19,7 @@ import WebKit
1919
2020internal class iOSVendorSystem : VendorSystem {
2121 private let device = UIDevice . current
22+ @Atomic private static var asyncUserAgent : String ? = nil
2223
2324 override var manufacturer : String {
2425 return " Apple "
@@ -65,17 +66,15 @@ internal class iOSVendorSystem: VendorSystem {
6566
6667 override var userAgent : String ? {
6768 #if !os(tvOS)
68- var userAgent : String ?
69-
70- if Thread . isMainThread {
71- userAgent = WKWebView ( ) . value ( forKey: " userAgent " ) as? String
72- } else {
73- DispatchQueue . main. sync {
74- userAgent = WKWebView ( ) . value ( forKey: " userAgent " ) as? String
69+ // BKS: It was discovered that on some platforms there can be a delay in retrieval.
70+ // It has to be fetched on the main thread, so we've spun it off
71+ // async and cache it when it comes back.
72+ if Self . asyncUserAgent == nil {
73+ DispatchQueue . main. async {
74+ Self . asyncUserAgent = WKWebView ( ) . value ( forKey: " userAgent " ) as? String
7575 }
7676 }
77-
78- return userAgent
77+ return Self . asyncUserAgent
7978 #else
8079 // webkit isn't on tvos
8180 return " unknown "
@@ -198,6 +197,7 @@ import WebKit
198197
199198internal class MacOSVendorSystem : VendorSystem {
200199 private let device = ProcessInfo . processInfo
200+ @Atomic private static var asyncUserAgent : String ? = nil
201201
202202 override var manufacturer : String {
203203 return " Apple "
@@ -238,16 +238,15 @@ internal class MacOSVendorSystem: VendorSystem {
238238 }
239239
240240 override var userAgent : String ? {
241- var userAgent : String ?
242- if Thread . isMainThread {
243- userAgent = WKWebView ( ) . value ( forKey : " userAgent " ) as? String
244- } else {
245- DispatchQueue . main. sync {
246- userAgent = WKWebView ( ) . value ( forKey: " userAgent " ) as? String
241+ // BKS: It was discovered that on some platforms there can be a delay in retrieval.
242+ // It has to be fetched on the main thread, so we've spun it off
243+ // async and cache it when it comes back.
244+ if Self . asyncUserAgent == nil {
245+ DispatchQueue . main. async {
246+ Self . asyncUserAgent = WKWebView ( ) . value ( forKey: " userAgent " ) as? String
247247 }
248248 }
249-
250- return userAgent
249+ return Self . asyncUserAgent
251250 }
252251
253252 override var connection : ConnectionStatus {
0 commit comments