Skip to content

Commit 487c97a

Browse files
authored
[Swift 6] Sessions Swift 6 changes (#14575)
1 parent 0f7ada0 commit 487c97a

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

FirebaseSessions/Sources/FirebaseSessions.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,14 @@ private enum GoogleDataTransportConfig {
149149

150150
super.init()
151151

152-
for subscriberName in SessionsDependencies.dependencies {
152+
let dependencies = SessionsDependencies.dependencies
153+
for subscriberName in dependencies {
153154
subscriberPromises[subscriberName] = Promise<Void>.pending()
154155
}
155156

156157
Logger
157158
.logDebug(
158-
"Version \(FirebaseVersion()). Expecting subscriptions from: \(SessionsDependencies.dependencies)"
159+
"Version \(FirebaseVersion()). Expecting subscriptions from: \(dependencies)"
159160
)
160161

161162
self.initiator.beginListening {

FirebaseSessions/Sources/Public/SessionsDependencies.swift

Lines changed: 53 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,62 @@
1515

1616
import Foundation
1717

18-
// Sessions Dependencies determines when a dependent SDK is
19-
// installed in the app. The Sessions SDK uses this to figure
20-
// out which dependencies to wait for to getting the data
21-
// collection state.
22-
//
23-
// This is important because the Sessions SDK starts up before
24-
// dependent SDKs
18+
private final class AtomicBox<T> {
19+
private var _value: T
20+
private let lock = NSLock()
21+
22+
init(_ value: T) {
23+
_value = value
24+
}
25+
26+
func value() -> T {
27+
lock.withLock {
28+
_value
29+
}
30+
}
31+
32+
@discardableResult func withLock(_ body: (_ value: inout T) -> Void) -> T {
33+
lock.withLock {
34+
body(&_value)
35+
return _value
36+
}
37+
}
38+
}
39+
40+
/// Sessions Dependencies determines when a dependent SDK is
41+
/// installed in the app. The Sessions SDK uses this to figure
42+
/// out which dependencies to wait for to getting the data
43+
/// collection state.
44+
///
45+
/// This is important because the Sessions SDK starts up before
46+
/// dependent SDKs
2547
@objc(FIRSessionsDependencies)
2648
public class SessionsDependencies: NSObject {
27-
static var dependencies: Set<SessionsSubscriberName> = .init()
49+
#if compiler(>=6)
50+
private nonisolated(unsafe) static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> =
51+
AtomicBox(
52+
Set()
53+
)
54+
#else
55+
private static let _dependencies: AtomicBox<Set<SessionsSubscriberName>> = AtomicBox(
56+
Set()
57+
)
58+
#endif
59+
60+
static var dependencies: Set<SessionsSubscriberName> {
61+
_dependencies.value()
62+
}
2863

2964
@objc public static func addDependency(name: SessionsSubscriberName) {
30-
SessionsDependencies.dependencies.insert(name)
65+
_dependencies.withLock { dependencies in
66+
dependencies.insert(name)
67+
}
68+
}
69+
70+
/// For testing only.
71+
static func removeAll() {
72+
_dependencies.withLock { dependencies in
73+
dependencies.removeAll()
74+
}
3175
}
3276
}

FirebaseSessions/Tests/Unit/Library/FirebaseSessionsTestsBase.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class FirebaseSessionsTestsBase: XCTestCase {
7777
postLogEvent: @escaping (Result<Void, FirebaseSessionsError>,
7878
[SessionsSubscriber]) -> Void) {
7979
// This class is static, so we need to clear global state
80-
SessionsDependencies.dependencies.removeAll()
80+
SessionsDependencies.removeAll()
8181

8282
for subscriberSDK in subscriberSDKs {
8383
SessionsDependencies.addDependency(name: subscriberSDK.sessionsSubscriberName)

0 commit comments

Comments
 (0)