Skip to content

Commit

Permalink
[ios] Remove dispatch_once_t used as a member instance (#7576)
Browse files Browse the repository at this point in the history
# Why

While investigating reasons for #7562 I [found out](https://stackoverflow.com/a/13858628/1123156) it's not recommended to use `dispatch_once_t` as a property.

# How

- removed all `dispatch_once_t` uses in favor of simple `if (!…) { … =`

# Test Plan

Expo client compiled, running `native-component-list#Permissions` screen worked ok.
  • Loading branch information
sjchmiela committed Apr 29, 2020
1 parent 9247291 commit 0dddd79
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 53 deletions.
Expand Up @@ -18,7 +18,6 @@
@interface ABI34_0_0UMExportedModule ()

@property (nonatomic, strong) dispatch_queue_t methodQueue;
@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *exportedMethods;

@end
Expand Down Expand Up @@ -55,14 +54,10 @@ - (NSDictionary *)constantsToExport

- (dispatch_queue_t)methodQueue
{
__weak ABI34_0_0UMExportedModule *weakSelf = self;
dispatch_once(&_methodQueueSetupOnce, ^{
__strong ABI34_0_0UMExportedModule *strongSelf = weakSelf;
if (strongSelf) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[strongSelf class] exportedModuleName]];
strongSelf.methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
});
if (!_methodQueue) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[self class] exportedModuleName]];
_methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
return _methodQueue;
}

Expand Down
Expand Up @@ -18,7 +18,6 @@
@interface ABI35_0_0UMExportedModule ()

@property (nonatomic, strong) dispatch_queue_t methodQueue;
@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *exportedMethods;

@end
Expand Down Expand Up @@ -55,14 +54,10 @@ - (NSDictionary *)constantsToExport

- (dispatch_queue_t)methodQueue
{
__weak ABI35_0_0UMExportedModule *weakSelf = self;
dispatch_once(&_methodQueueSetupOnce, ^{
__strong ABI35_0_0UMExportedModule *strongSelf = weakSelf;
if (strongSelf) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[strongSelf class] exportedModuleName]];
strongSelf.methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
});
if (!_methodQueue) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[self class] exportedModuleName]];
_methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
return _methodQueue;
}

Expand Down
Expand Up @@ -18,7 +18,6 @@
@interface ABI36_0_0UMExportedModule ()

@property (nonatomic, strong) dispatch_queue_t methodQueue;
@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *exportedMethods;

@end
Expand Down Expand Up @@ -55,14 +54,10 @@ - (NSDictionary *)constantsToExport

- (dispatch_queue_t)methodQueue
{
__weak ABI36_0_0UMExportedModule *weakSelf = self;
dispatch_once(&_methodQueueSetupOnce, ^{
__strong ABI36_0_0UMExportedModule *strongSelf = weakSelf;
if (strongSelf) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[strongSelf class] exportedModuleName]];
strongSelf.methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
});
if (!_methodQueue) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[self class] exportedModuleName]];
_methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
return _methodQueue;
}

Expand Down
Expand Up @@ -20,7 +20,6 @@ @interface ABI37_0_0EXPermissions ()
@property (nonatomic, strong) NSMutableDictionary<NSString *, id<ABI37_0_0UMPermissionsRequester>> *requesters;
@property (nonatomic, strong) NSMapTable<Class, id<ABI37_0_0UMPermissionsRequester>> *requestersByClass;
@property (nonatomic, weak) ABI37_0_0UMModuleRegistry *moduleRegistry;
@property (nonatomic) dispatch_once_t requestersFallbacksRegisteredOnce;

@end

Expand Down Expand Up @@ -209,9 +208,7 @@ + (ABI37_0_0UMPermissionStatus)statusForPermission:(NSDictionary *)permission

- (id<ABI37_0_0UMPermissionsRequester>)getPermissionRequesterForType:(NSString *)type
{
dispatch_once(&_requestersFallbacksRegisteredOnce, ^{
[self ensureRequestersFallbacksAreRegistered];
});
[self ensureRequestersFallbacksAreRegistered];
return _requesters[type];
}

Expand Down
Expand Up @@ -18,7 +18,6 @@
@interface ABI37_0_0UMExportedModule ()

@property (nonatomic, strong) dispatch_queue_t methodQueue;
@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *exportedMethods;

@end
Expand Down Expand Up @@ -55,14 +54,10 @@ - (NSDictionary *)constantsToExport

- (dispatch_queue_t)methodQueue
{
__weak ABI37_0_0UMExportedModule *weakSelf = self;
dispatch_once(&_methodQueueSetupOnce, ^{
__strong ABI37_0_0UMExportedModule *strongSelf = weakSelf;
if (strongSelf) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[strongSelf class] exportedModuleName]];
strongSelf.methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
});
if (!_methodQueue) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[self class] exportedModuleName]];
_methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
return _methodQueue;
}

Expand Down
2 changes: 2 additions & 0 deletions packages/@unimodules/core/CHANGELOG.md
Expand Up @@ -7,3 +7,5 @@
### 🎉 New features

### 🐛 Bug fixes

- Fixed a rare undetermined behavior that may have been a result of misuse of `dispatch_once_t` on iOS ([#7576](https://github.com/expo/expo/pull/7576) by [@sjchmiela](https://github.com/sjchmiela))
13 changes: 4 additions & 9 deletions packages/@unimodules/core/ios/UMCore/UMExportedModule.m
Expand Up @@ -18,7 +18,6 @@
@interface UMExportedModule ()

@property (nonatomic, strong) dispatch_queue_t methodQueue;
@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce;
@property (nonatomic, strong) NSDictionary<NSString *, NSString *> *exportedMethods;

@end
Expand Down Expand Up @@ -55,14 +54,10 @@ - (NSDictionary *)constantsToExport

- (dispatch_queue_t)methodQueue
{
__weak UMExportedModule *weakSelf = self;
dispatch_once(&_methodQueueSetupOnce, ^{
__strong UMExportedModule *strongSelf = weakSelf;
if (strongSelf) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[strongSelf class] exportedModuleName]];
strongSelf.methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
});
if (!_methodQueue) {
NSString *queueName = [NSString stringWithFormat:@"org.unimodules.%@Queue", [[self class] exportedModuleName]];
_methodQueue = dispatch_queue_create(queueName.UTF8String, DISPATCH_QUEUE_SERIAL);
}
return _methodQueue;
}

Expand Down
1 change: 1 addition & 0 deletions packages/expo-permissions/CHANGELOG.md
Expand Up @@ -10,3 +10,4 @@

- Fix permissions in the headless mode. ([#7962](https://github.com/expo/expo/pull/7962) by [@lukmccall](https://github.com/lukmccall))
- Fixed `permission cannot be null or empty` error when asking for `WRITE_SETTINGS` permission on Android. ([#7276](https://github.com/expo/expo/pull/7276) by [@lukmccall](https://github.com/lukmccall))
- Fixed a rare undetermined behavior that may have been a result of misuse of `dispatch_once_t` on iOS ([#7576](https://github.com/expo/expo/pull/7576) by [@sjchmiela](https://github.com/sjchmiela))
5 changes: 1 addition & 4 deletions packages/expo-permissions/ios/EXPermissions/EXPermissions.m
Expand Up @@ -20,7 +20,6 @@ @interface EXPermissions ()
@property (nonatomic, strong) NSMutableDictionary<NSString *, id<UMPermissionsRequester>> *requesters;
@property (nonatomic, strong) NSMapTable<Class, id<UMPermissionsRequester>> *requestersByClass;
@property (nonatomic, weak) UMModuleRegistry *moduleRegistry;
@property (nonatomic) dispatch_once_t requestersFallbacksRegisteredOnce;

@end

Expand Down Expand Up @@ -209,9 +208,7 @@ + (UMPermissionStatus)statusForPermission:(NSDictionary *)permission

- (id<UMPermissionsRequester>)getPermissionRequesterForType:(NSString *)type
{
dispatch_once(&_requestersFallbacksRegisteredOnce, ^{
[self ensureRequestersFallbacksAreRegistered];
});
[self ensureRequestersFallbacksAreRegistered];
return _requesters[type];
}

Expand Down

0 comments on commit 0dddd79

Please sign in to comment.