Skip to content

Commit c02d876

Browse files
bookerbooker
authored andcommitted
Store handlers in dictionary to prevent ARC
1 parent 3570e25 commit c02d876

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

ios/RNPermissions.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ typedef enum {
7979

8080
@interface RNPermissions : NSObject <RCTBridgeModule>
8181

82+
@property (nonatomic, strong) NSMutableDictionary<NSString *, id<RNPermissionHandler>> *_Nonnull handlers;
83+
8284
+ (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId;
8385

8486
+ (void)flagAsRequested:(NSString * _Nonnull)handlerId;

ios/RNPermissions.m

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ - (NSString *)stringForStatus:(RNPermissionStatus)status {
225225
}
226226
}
227227

228+
- (NSString *)insertHandler:(id<RNPermissionHandler>)handler {
229+
if( self.handlers == nil){
230+
self.handlers = [NSMutableDictionary new];
231+
}
232+
233+
NSString *randomId = [[NSUUID UUID] UUIDString];
234+
235+
[self.handlers setObject:handler forKey:randomId];
236+
237+
return randomId;
238+
}
239+
228240
+ (bool)isFlaggedAsRequested:(NSString * _Nonnull)handlerId {
229241
NSArray<NSString *> *requested = [[NSUserDefaults standardUserDefaults] arrayForKey:SETTING_KEY];
230242
return requested == nil ? false : [requested containsObject:handlerId];
@@ -271,13 +283,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
271283
rejecter:(RCTPromiseRejectBlock)reject) {
272284
id<RNPermissionHandler> handler = [self handlerForPermission:permission];
273285

286+
NSString *randomId = [self insertHandler: handler];
287+
274288
[handler checkWithResolver:^(RNPermissionStatus status) {
275289
NSString *strStatus = [self stringForStatus:status];
276290
NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
277291
resolve(strStatus);
292+
293+
[self.handlers removeObjectForKey:randomId];
278294
} rejecter:^(NSError *error) {
279295
NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
280296
reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
297+
298+
[self.handlers removeObjectForKey:randomId];
281299
}];
282300
}
283301

@@ -287,13 +305,19 @@ + (void)flagAsRequested:(NSString * _Nonnull)handlerId {
287305
rejecter:(RCTPromiseRejectBlock)reject) {
288306
id<RNPermissionHandler> handler = [self handlerForPermission:permission];
289307

308+
NSString *randomId = [self insertHandler: handler];
309+
290310
[handler requestWithResolver:^(RNPermissionStatus status) {
291311
NSString *strStatus = [self stringForStatus:status];
292312
NSLog(@"[react-native-permissions] %@ permission checked: %@", [[handler class] handlerUniqueId], strStatus);
293313
resolve(strStatus);
314+
315+
[self.handlers removeObjectForKey:randomId];
294316
} rejecter:^(NSError *error) {
295317
NSLog(@"[react-native-permissions] %@ permission failed: %@", [[handler class] handlerUniqueId], error.localizedDescription);
296318
reject([NSString stringWithFormat:@"%ld", (long)error.code], error.localizedDescription, error);
319+
320+
[self.handlers removeObjectForKey:randomId];
297321
}];
298322
}
299323

0 commit comments

Comments
 (0)