Quellcode durchsuchen

Remove retain cycles from selectionHandler in several files

matrush vor 6 Jahren
Ursprung
Commit
28e91507db

+ 20 - 16
Classes/GlobalStateExplorers/FLEXObjectListViewController.m

@@ -125,14 +125,14 @@
             }
         }
     }];
-    
+
     NSArray<FLEXObjectRef *> *references = [FLEXObjectRef referencingAll:instances];
     if (references.count == 1) {
         return [FLEXObjectExplorerFactory
                 explorerViewControllerForObject:references.firstObject.object
         ];
     }
-    
+
     FLEXObjectListViewController *controller = [[self alloc] initWithReferences:references];
     controller.title = [NSString stringWithFormat:@"%@ (%lu)", className, (unsigned long)instances.count];
     return controller;
@@ -145,7 +145,7 @@
     controller.title = [NSString stringWithFormat:@"Subclasses of %@ (%lu)",
         className, (unsigned long)classes.count
     ];
-    
+
     return controller;
 }
 
@@ -158,7 +158,7 @@
             SwiftObjectClass = NSClassFromString(@"Swift._SwiftObject");
         }
     });
-    
+
     NSMutableArray<FLEXObjectRef *> *instances = [NSMutableArray new];
     [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id tryObject, __unsafe_unretained Class actualClass) {
         // Get all the ivars on the object. Start with the class and and travel up the inheritance chain.
@@ -167,15 +167,15 @@
         while (tryClass) {
             unsigned int ivarCount = 0;
             Ivar *ivars = class_copyIvarList(tryClass, &ivarCount);
-            
+
             for (unsigned int ivarIndex = 0; ivarIndex < ivarCount; ivarIndex++) {
                 Ivar ivar = ivars[ivarIndex];
                 NSString *typeEncoding = @(ivar_getTypeEncoding(ivar) ?: "");
-                
+
                 if (typeEncoding.flex_typeIsObjectOrClass) {
                     ptrdiff_t offset = ivar_getOffset(ivar);
                     uintptr_t *fieldPointer = (__bridge void *)tryObject + offset;
-                    
+
                     if (*fieldPointer == (uintptr_t)(__bridge void *)object) {
                         NSString *ivarName = @(ivar_getName(ivar) ?: "???");
                         [instances addObject:[FLEXObjectRef referencing:tryObject ivar:ivarName]];
@@ -183,7 +183,7 @@
                     }
                 }
             }
-            
+
             tryClass = class_getSuperclass(tryClass);
         }
     }];
@@ -206,7 +206,7 @@
 
 - (void)viewDidLoad {
     [super viewDidLoad];
-    
+
     self.showsSearchBar = YES;
 }
 
@@ -224,7 +224,7 @@
 - (NSArray *)buildSections:(NSArray<NSString *> *)titles predicates:(NSArray<NSPredicate *> *)predicates {
     NSParameterAssert(titles.count == predicates.count);
     NSParameterAssert(titles); NSParameterAssert(predicates);
-    
+
     return [NSArray flex_forEachUpTo:titles.count map:^id(NSUInteger i) {
         NSArray *rows = [self.references filteredArrayUsingPredicate:predicates[i]];
         return [self makeSection:rows title:titles[i]];
@@ -241,18 +241,22 @@
             if (ref.summary && [ref.summary localizedCaseInsensitiveContainsString:filterText]) {
                 return YES;
             }
-            
+
             return [ref.reference localizedCaseInsensitiveContainsString:filterText];
         }
     ];
-    
+
+    __weak __typeof(self) weakSelf = self;
     section.selectionHandler = ^(__kindof UIViewController *host, FLEXObjectRef *ref) {
-        [self.navigationController pushViewController:[
-            FLEXObjectExplorerFactory explorerViewControllerForObject:ref.object
-        ] animated:YES];
+        __strong __typeof(self) strongSelf = weakSelf;
+        if (strongSelf) {
+            [strongSelf.navigationController pushViewController:[
+                FLEXObjectExplorerFactory explorerViewControllerForObject:ref.object
+            ] animated:YES];
+        }
     };
 
-    section.customTitle = title;    
+    section.customTitle = title;
     return section;
 }
 

+ 10 - 6
Classes/ObjectExplorers/Sections/FLEXMutableListSection.m

@@ -29,12 +29,12 @@ configurationBlock:(FLEXMutableListCellForElement)cellConfig
     self = [super init];
     if (self) {
         _configureCell = cellConfig;
-        
+
         self.list = list.mutableCopy;
         self.customFilter = filterBlock;
         self.hideSectionTitle = YES;
     }
-    
+
     return self;
 }
 
@@ -48,7 +48,7 @@ configurationBlock:(FLEXMutableListCellForElement)cellConfig
 - (void)setList:(NSMutableArray *)list {
     NSParameterAssert(list);
     _collection = list;
-    
+
     [self reloadData];
 }
 
@@ -79,11 +79,15 @@ configurationBlock:(FLEXMutableListCellForElement)cellConfig
 
 - (void (^)(__kindof UIViewController *))didSelectRowAction:(NSInteger)row {
     if (self.selectionHandler) {
+        __weak __typeof(self) weakSelf = self;
         return ^(UIViewController *host) {
-            self.selectionHandler(host, self.filteredList[row]);
+            __strong __typeof(self) strongSelf = weakSelf;
+            if (strongSelf) {
+                strongSelf.selectionHandler(host, strongSelf.filteredList[row]);
+            }
         };
     }
-    
+
     return nil;
 }
 
@@ -95,7 +99,7 @@ configurationBlock:(FLEXMutableListCellForElement)cellConfig
     if (self.cellRegistrationMapping.count) {
         return self.cellRegistrationMapping.allKeys.firstObject;
     }
-    
+
     return [super reuseIdentifierForRow:row];
 }
 

+ 9 - 5
Classes/ObjectExplorers/Sections/Shortcuts/FLEXBundleShortcuts.m

@@ -18,6 +18,7 @@
 #pragma mark Overrides
 
 + (instancetype)forObject:(NSBundle *)bundle {
+    __weak __typeof(self) weakSelf = self;
     return [self forObject:bundle additionalRows:@[
         [FLEXActionShortcut
             title:@"Browse Bundle Directory" subtitle:nil
@@ -30,7 +31,10 @@
         ],
         [FLEXActionShortcut title:@"Browse Bundle as Database…" subtitle:nil
             selectionHandler:^(UIViewController *host, NSBundle *bundle) {
-                [self promptToExportBundleAsDatabase:bundle host:host];
+                __strong __typeof(self) strongSelf = weakSelf;
+                if (strongSelf) {
+                    [strongSelf promptToExportBundleAsDatabase:bundle host:host];
+                }
             }
             accessoryType:^UITableViewCellAccessoryType(NSBundle *bundle) {
                 return UITableViewCellAccessoryDisclosureIndicator;
@@ -62,22 +66,22 @@
 
 + (void)browseBundleAsDatabase:(NSBundle *)bundle host:(UIViewController *)host name:(NSString *)name {
     NSParameterAssert(name.length);
-    
+
     UIAlertController *progress = [FLEXAlert makeAlert:^(FLEXAlert *make) {
         make.title(@"Generating Database");
         // Some iOS version glitch out of there is
         // no initial message and you add one later
         make.message(@"…");
     }];
-    
+
     [host presentViewController:progress animated:YES completion:^{
         // Generate path to store db
         NSString *path = [NSSearchPathForDirectoriesInDomains(
             NSLibraryDirectory, NSUserDomainMask, YES
         )[0] stringByAppendingPathComponent:name];
-        
+
         progress.message = [path stringByAppendingString:@"\n\nCreating database…"];
-        
+
         // Generate db and show progress
         [FLEXRuntimeExporter createRuntimeDatabaseAtPath:path
             forImages:@[bundle.executablePath]