16
16
import Foundation
17
17
18
18
internal import FirebaseInstallations
19
+ internal import FirebaseCoreInternal
19
20
20
21
protocol InstallationsProtocol : Sendable {
21
22
var installationsWaitTimeInSecond : Int { get }
22
23
23
24
/// Override Installation function for testing
24
- func authToken( completion: @escaping ( InstallationsAuthTokenResult ? , Error ? ) -> Void )
25
+ func authToken( completion: @escaping @ Sendable ( InstallationsAuthTokenResult ? , Error ? ) -> Void )
25
26
26
27
/// Override Installation function for testing
27
- func installationID( completion: @escaping ( String ? , Error ? ) -> Void )
28
+ func installationID( completion: @escaping @ Sendable ( String ? , Error ? ) -> Void )
28
29
29
30
/// Return a tuple: (installationID, authenticationToken) for success result
30
31
func installationID( completion: @escaping ( Result < ( String , String ) , Error > ) -> Void )
@@ -35,25 +36,27 @@ extension InstallationsProtocol {
35
36
return 10
36
37
}
37
38
39
+ // TODO(ncooke3): Convert o async await ahead of Firebase 12.
40
+
38
41
func installationID( completion: @escaping ( Result < ( String , String ) , Error > ) -> Void ) {
39
- var authTokenComplete = " "
40
- var installationComplete : String ?
41
- var errorComplete : Error ?
42
+ let authTokenComplete = FIRAllocatedUnfairLock < String > ( initialState : " " )
43
+ let installationComplete = FIRAllocatedUnfairLock < String ? > ( initialState : nil )
44
+ let errorComplete = FIRAllocatedUnfairLock < Error ? > ( initialState : nil )
42
45
43
46
let workingGroup = DispatchGroup ( )
44
47
45
48
workingGroup. enter ( )
46
49
authToken { ( authTokenResult: InstallationsAuthTokenResult ? , error: Error ? ) in
47
- authTokenComplete = authTokenResult? . authToken ?? " "
50
+ authTokenComplete. withLock { $0 = authTokenResult? . authToken ?? " " }
48
51
workingGroup. leave ( )
49
52
}
50
53
51
54
workingGroup. enter ( )
52
55
installationID { ( installationID: String ? , error: Error ? ) in
53
56
if let installationID {
54
- installationComplete = installationID
55
- } else if let error = error {
56
- errorComplete = error
57
+ installationComplete. withLock { $0 = installationID }
58
+ } else if let error {
59
+ errorComplete. withLock { $0 = error }
57
60
}
58
61
workingGroup. leave ( )
59
62
}
@@ -67,9 +70,9 @@ extension InstallationsProtocol {
67
70
completion ( . failure( FirebaseSessionsError . SessionInstallationsTimeOutError) )
68
71
return
69
72
default :
70
- if let installationComplete {
71
- completion ( . success( ( installationComplete, authTokenComplete) ) )
72
- } else if let errorComplete {
73
+ if let installationComplete = installationComplete . value ( ) {
74
+ completion ( . success( ( installationComplete, authTokenComplete. value ( ) ) ) )
75
+ } else if let errorComplete = errorComplete . value ( ) {
73
76
completion ( . failure( errorComplete) )
74
77
}
75
78
}
0 commit comments