|
16 | 16 |
|
17 | 17 | #import "Firestore/Source/Core/FSTFirestoreClient.h"
|
18 | 18 |
|
| 19 | +#import <future> |
| 20 | + |
19 | 21 | #import "Firestore/Source/Core/FSTEventManager.h"
|
20 | 22 | #import "Firestore/Source/Core/FSTSyncEngine.h"
|
21 | 23 | #import "Firestore/Source/Core/FSTTransaction.h"
|
@@ -104,35 +106,31 @@ - (instancetype)initWithDatabaseInfo:(const DatabaseInfo &)databaseInfo
|
104 | 106 | _userDispatchQueue = userDispatchQueue;
|
105 | 107 | _workerDispatchQueue = workerDispatchQueue;
|
106 | 108 |
|
107 |
| - dispatch_semaphore_t initialUserAvailable = dispatch_semaphore_create(0); |
108 |
| - __block bool initialized = false; |
109 |
| - __block User initialUser; |
110 |
| - FSTWeakify(self); |
111 |
| - auto userChangeListener = ^(User user) { |
112 |
| - FSTStrongify(self); |
113 |
| - if (self) { |
114 |
| - if (!initialized) { |
115 |
| - initialUser = user; |
116 |
| - initialized = true; |
117 |
| - dispatch_semaphore_signal(initialUserAvailable); |
118 |
| - } else { |
119 |
| - [workerDispatchQueue dispatchAsync:^{ |
120 |
| - [self userDidChange:user]; |
121 |
| - }]; |
122 |
| - } |
| 109 | + auto userPromise = std::make_shared<std::promise<User>>(); |
| 110 | + |
| 111 | + __weak typeof(self) weakSelf = self; |
| 112 | + auto userChangeListener = [initialized = false, userPromise, weakSelf, workerDispatchQueue](User user) mutable { |
| 113 | + typeof(self) strongSelf = weakSelf; |
| 114 | + if (!strongSelf) return; |
| 115 | + |
| 116 | + if (!initialized) { |
| 117 | + initialized = true; |
| 118 | + userPromise->set_value(user); |
| 119 | + } else { |
| 120 | + [workerDispatchQueue dispatchAsync:^{ |
| 121 | + [strongSelf userDidChange:user]; |
| 122 | + }]; |
123 | 123 | }
|
124 | 124 | };
|
125 | 125 |
|
126 |
| - _credentialsProvider->SetUserChangeListener( |
127 |
| - [userChangeListener](const User &user) { userChangeListener(user); }); |
| 126 | + _credentialsProvider->SetUserChangeListener(userChangeListener); |
128 | 127 |
|
129 | 128 | // Defer initialization until we get the current user from the userChangeListener. This is
|
130 | 129 | // guaranteed to be synchronously dispatched onto our worker queue, so we will be initialized
|
131 | 130 | // before any subsequently queued work runs.
|
132 | 131 | [_workerDispatchQueue dispatchAsync:^{
|
133 |
| - dispatch_semaphore_wait(initialUserAvailable, DISPATCH_TIME_FOREVER); |
134 |
| - |
135 |
| - [self initializeWithUser:initialUser usePersistence:usePersistence]; |
| 132 | + User user = userPromise->get_future().get(); |
| 133 | + [self initializeWithUser:user usePersistence:usePersistence]; |
136 | 134 | }];
|
137 | 135 | }
|
138 | 136 | return self;
|
@@ -237,7 +235,7 @@ - (void)enableNetworkWithCompletion:(nullable FSTVoidErrorBlock)completion {
|
237 | 235 |
|
238 | 236 | - (void)shutdownWithCompletion:(nullable FSTVoidErrorBlock)completion {
|
239 | 237 | [self.workerDispatchQueue dispatchAsync:^{
|
240 |
| - _credentialsProvider->SetUserChangeListener(nullptr); |
| 238 | + self->_credentialsProvider->SetUserChangeListener(nullptr); |
241 | 239 |
|
242 | 240 | [self.remoteStore shutdown];
|
243 | 241 | [self.localStore shutdown];
|
|
0 commit comments