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
 
104
 
105
 @end
105
 @end
106
 
106
 
107
+@interface RNPermissions ()
108
+@property (nonatomic, strong) NSMutableDictionary<NSString *, id<RNPermissionHandler>>  *_Nonnull handlers;
109
+@end
110
+
107
 @implementation RNPermissions
111
 @implementation RNPermissions
108
 
112
 
109
 RCT_EXPORT_MODULE();
113
 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
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
244
 + (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
229
   NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
245
   NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
230
   return requested == nil ? false : [requested containsObject:handlerId];
246
   return requested == nil ? false : [requested containsObject:handlerId];
271
                  rejecter:(RCTPromiseRejectBlock)reject) {
287
                  rejecter:(RCTPromiseRejectBlock)reject) {
272
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
288
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
273
 
289
 
290
+  NSString *randomId = [self insertHandler: handler];
291
+
274
   [handler checkWithResolver:^(RNPermissionStatus status) {
292
   [handler checkWithResolver:^(RNPermissionStatus status) {
275
     NSString *strStatus = [self stringForStatus:status];
293
     NSString *strStatus = [self stringForStatus:status];
276
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
294
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
277
     resolve(strStatus);
295
     resolve(strStatus);
296
+
297
+    [self.handlers removeObjectForKey:randomId];
278
   } rejecter:^(NSError *error) {
298
   } rejecter:^(NSError *error) {
279
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
299
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
280
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
300
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
301
+
302
+    [self.handlers removeObjectForKey:randomId];
281
   }];
303
   }];
282
 }
304
 }
283
 
305
 
287
                  rejecter:(RCTPromiseRejectBlock)reject) {
309
                  rejecter:(RCTPromiseRejectBlock)reject) {
288
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
310
   id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289
 
311
 
312
+  NSString *randomId = [self insertHandler: handler];
313
+
290
   [handler requestWithResolver:^(RNPermissionStatus status) {
314
   [handler requestWithResolver:^(RNPermissionStatus status) {
291
     NSString *strStatus = [self stringForStatus:status];
315
     NSString *strStatus = [self stringForStatus:status];
292
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
316
     NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
293
     resolve(strStatus);
317
     resolve(strStatus);
318
+
319
+    [self.handlers removeObjectForKey:randomId];
294
   } rejecter:^(NSError *error) {
320
   } rejecter:^(NSError *error) {
295
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
321
     NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
296
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
322
     reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
323
+
324
+    [self.handlers removeObjectForKey:randomId];
297
   }];
325
   }];
298
 }
326
 }
299
 
327