Browse Source

Merge pull request #391 from booker0108/hotfix/refactor-handler-scope-to-prevent-aggressive-ARC

Hotfix: Prevent handler being ARC on iOS before resolving the request
Mathieu Acthernoene 5 years ago
parent
commit
570ab886a8
No account linked to committer's email address
1 changed files with 28 additions and 0 deletions
  1. 28
    0
      ios/RNPermissions.m

+ 28
- 0
ios/RNPermissions.m View File

@@ -104,6 +104,10 @@ RCT_ENUM_CONVERTER(RNPermission, (@{
104 104
 
105 105
 @end
106 106
 
107
+@interface RNPermissions ()
108
+@property (nonatomic, strong) NSMutableDictionary<NSString *, id<RNPermissionHandler>>  *_Nonnull handlers;
109
+@end
110
+
107 111
 @implementation RNPermissions
108 112
 
109 113
 RCT_EXPORT_MODULE();
@@ -225,6 +229,18 @@ RCT_EXPORT_MODULE();
225 229
   }
226 230
 }
227 231
 
232
+- (NSString *)insertHandler:(id<RNPermissionHandler>)handler {
233
+    if(_handlers == nil){
234
+        _handlers = [NSMutableDictionary new];
235
+    }
236
+    
237
+    NSString *randomId = [[NSUUID UUID] UUIDString];
238
+    
239
+    [_handlers setObject:handler forKey:randomId];
240
+    
241
+    return randomId;
242
+}
243
+
228 244
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
229 245
   NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
230 246
   return requested == nil ? false : [requested containsObject:handlerId];
@@ -271,13 +287,19 @@ RCT_REMAP_METHOD(check,
271 287
                  rejecter:(RCTPromiseRejectBlock)reject) {
272 288
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
273 289
 
290
+  NSString *randomId = [self insertHandler: handler];
291
+
274 292
   [handler checkWithResolver:^(RNPermissionStatus status) {
275 293
     NSString *strStatus = [self stringForStatus:status];
276 294
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
277 295
     resolve(strStatus);
296
+
297
+    [self.handlers removeObjectForKey:randomId];
278 298
   } rejecter:^(NSError *error) {
279 299
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
280 300
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
301
+
302
+    [self.handlers removeObjectForKey:randomId];
281 303
   }];
282 304
 }
283 305
 
@@ -287,13 +309,19 @@ RCT_REMAP_METHOD(request,
287 309
                  rejecter:(RCTPromiseRejectBlock)reject) {
288 310
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289 311
 
312
+  NSString *randomId = [self insertHandler: handler];
313
+
290 314
   [handler requestWithResolver:^(RNPermissionStatus status) {
291 315
     NSString *strStatus = [self stringForStatus:status];
292 316
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
293 317
     resolve(strStatus);
318
+
319
+    [self.handlers removeObjectForKey:randomId];
294 320
   } rejecter:^(NSError *error) {
295 321
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
296 322
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
323
+
324
+    [self.handlers removeObjectForKey:randomId];
297 325
   }];
298 326
 }
299 327