From 0dddd79e5560d93c6590cba8a68bf1925757c92a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stanis=C5=82aw=20Chmiela?= Date: Wed, 29 Apr 2020 13:57:06 +0200 Subject: [PATCH] [ios] Remove dispatch_once_t used as a member instance (#7576) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Why While investigating reasons for https://github.com/expo/expo/issues/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. --- .../ABI34_0_0UMCore/ABI34_0_0UMExportedModule.m | 13 ++++--------- .../ABI35_0_0UMCore/ABI35_0_0UMExportedModule.m | 13 ++++--------- .../ABI36_0_0UMCore/ABI36_0_0UMExportedModule.m | 13 ++++--------- .../ABI37_0_0EXPermissions/ABI37_0_0EXPermissions.m | 5 +---- .../ABI37_0_0UMCore/ABI37_0_0UMExportedModule.m | 13 ++++--------- packages/@unimodules/core/CHANGELOG.md | 2 ++ .../@unimodules/core/ios/UMCore/UMExportedModule.m | 13 ++++--------- packages/expo-permissions/CHANGELOG.md | 1 + .../ios/EXPermissions/EXPermissions.m | 5 +---- 9 files changed, 25 insertions(+), 53 deletions(-) diff --git a/ios/versioned-react-native/ABI34_0_0/UMCore/ABI34_0_0UMCore/ABI34_0_0UMExportedModule.m b/ios/versioned-react-native/ABI34_0_0/UMCore/ABI34_0_0UMCore/ABI34_0_0UMExportedModule.m index 619c638d4fe40..3c48d0cb58b21 100644 --- a/ios/versioned-react-native/ABI34_0_0/UMCore/ABI34_0_0UMCore/ABI34_0_0UMExportedModule.m +++ b/ios/versioned-react-native/ABI34_0_0/UMCore/ABI34_0_0UMCore/ABI34_0_0UMExportedModule.m @@ -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 *exportedMethods; @end @@ -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; } diff --git a/ios/versioned-react-native/ABI35_0_0/UMCore/ABI35_0_0UMCore/ABI35_0_0UMExportedModule.m b/ios/versioned-react-native/ABI35_0_0/UMCore/ABI35_0_0UMCore/ABI35_0_0UMExportedModule.m index 095bbdeacf389..0cff30f012ab7 100644 --- a/ios/versioned-react-native/ABI35_0_0/UMCore/ABI35_0_0UMCore/ABI35_0_0UMExportedModule.m +++ b/ios/versioned-react-native/ABI35_0_0/UMCore/ABI35_0_0UMCore/ABI35_0_0UMExportedModule.m @@ -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 *exportedMethods; @end @@ -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; } diff --git a/ios/versioned-react-native/ABI36_0_0/Expo/UMCore/ABI36_0_0UMCore/ABI36_0_0UMExportedModule.m b/ios/versioned-react-native/ABI36_0_0/Expo/UMCore/ABI36_0_0UMCore/ABI36_0_0UMExportedModule.m index 5011411953e68..f2a326b055574 100644 --- a/ios/versioned-react-native/ABI36_0_0/Expo/UMCore/ABI36_0_0UMCore/ABI36_0_0UMExportedModule.m +++ b/ios/versioned-react-native/ABI36_0_0/Expo/UMCore/ABI36_0_0UMCore/ABI36_0_0UMExportedModule.m @@ -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 *exportedMethods; @end @@ -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; } diff --git a/ios/versioned-react-native/ABI37_0_0/Expo/EXPermissions/ABI37_0_0EXPermissions/ABI37_0_0EXPermissions.m b/ios/versioned-react-native/ABI37_0_0/Expo/EXPermissions/ABI37_0_0EXPermissions/ABI37_0_0EXPermissions.m index 44d6f5bba1cb7..33c3c9e2288bd 100644 --- a/ios/versioned-react-native/ABI37_0_0/Expo/EXPermissions/ABI37_0_0EXPermissions/ABI37_0_0EXPermissions.m +++ b/ios/versioned-react-native/ABI37_0_0/Expo/EXPermissions/ABI37_0_0EXPermissions/ABI37_0_0EXPermissions.m @@ -20,7 +20,6 @@ @interface ABI37_0_0EXPermissions () @property (nonatomic, strong) NSMutableDictionary> *requesters; @property (nonatomic, strong) NSMapTable> *requestersByClass; @property (nonatomic, weak) ABI37_0_0UMModuleRegistry *moduleRegistry; -@property (nonatomic) dispatch_once_t requestersFallbacksRegisteredOnce; @end @@ -209,9 +208,7 @@ + (ABI37_0_0UMPermissionStatus)statusForPermission:(NSDictionary *)permission - (id)getPermissionRequesterForType:(NSString *)type { - dispatch_once(&_requestersFallbacksRegisteredOnce, ^{ - [self ensureRequestersFallbacksAreRegistered]; - }); + [self ensureRequestersFallbacksAreRegistered]; return _requesters[type]; } diff --git a/ios/versioned-react-native/ABI37_0_0/Expo/UMCore/ABI37_0_0UMCore/ABI37_0_0UMExportedModule.m b/ios/versioned-react-native/ABI37_0_0/Expo/UMCore/ABI37_0_0UMCore/ABI37_0_0UMExportedModule.m index 1102647fa1c35..a9a0b43059845 100644 --- a/ios/versioned-react-native/ABI37_0_0/Expo/UMCore/ABI37_0_0UMCore/ABI37_0_0UMExportedModule.m +++ b/ios/versioned-react-native/ABI37_0_0/Expo/UMCore/ABI37_0_0UMCore/ABI37_0_0UMExportedModule.m @@ -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 *exportedMethods; @end @@ -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; } diff --git a/packages/@unimodules/core/CHANGELOG.md b/packages/@unimodules/core/CHANGELOG.md index 6a294c6841ff4..1f147070bb918 100644 --- a/packages/@unimodules/core/CHANGELOG.md +++ b/packages/@unimodules/core/CHANGELOG.md @@ -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)) diff --git a/packages/@unimodules/core/ios/UMCore/UMExportedModule.m b/packages/@unimodules/core/ios/UMCore/UMExportedModule.m index 3f99fd39a379e..f23734fc0e782 100644 --- a/packages/@unimodules/core/ios/UMCore/UMExportedModule.m +++ b/packages/@unimodules/core/ios/UMCore/UMExportedModule.m @@ -18,7 +18,6 @@ @interface UMExportedModule () @property (nonatomic, strong) dispatch_queue_t methodQueue; -@property (nonatomic, assign) dispatch_once_t methodQueueSetupOnce; @property (nonatomic, strong) NSDictionary *exportedMethods; @end @@ -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; } diff --git a/packages/expo-permissions/CHANGELOG.md b/packages/expo-permissions/CHANGELOG.md index f2a1ced093d91..47643e2977bb6 100644 --- a/packages/expo-permissions/CHANGELOG.md +++ b/packages/expo-permissions/CHANGELOG.md @@ -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)) diff --git a/packages/expo-permissions/ios/EXPermissions/EXPermissions.m b/packages/expo-permissions/ios/EXPermissions/EXPermissions.m index e7ec21e0d160c..ea67c695485a7 100644 --- a/packages/expo-permissions/ios/EXPermissions/EXPermissions.m +++ b/packages/expo-permissions/ios/EXPermissions/EXPermissions.m @@ -20,7 +20,6 @@ @interface EXPermissions () @property (nonatomic, strong) NSMutableDictionary> *requesters; @property (nonatomic, strong) NSMapTable> *requestersByClass; @property (nonatomic, weak) UMModuleRegistry *moduleRegistry; -@property (nonatomic) dispatch_once_t requestersFallbacksRegisteredOnce; @end @@ -209,9 +208,7 @@ + (UMPermissionStatus)statusForPermission:(NSDictionary *)permission - (id)getPermissionRequesterForType:(NSString *)type { - dispatch_once(&_requestersFallbacksRegisteredOnce, ^{ - [self ensureRequestersFallbacksAreRegistered]; - }); + [self ensureRequestersFallbacksAreRegistered]; return _requesters[type]; }