FLEXLiveObjectsTableViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // FLEXLiveObjectsTableViewController.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 5/28/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import "FLEXLiveObjectsTableViewController.h"
  9. #import "FLEXHeapEnumerator.h"
  10. #import "FLEXInstancesTableViewController.h"
  11. #import "FLEXUtility.h"
  12. #import <objc/runtime.h>
  13. static const NSInteger kFLEXLiveObjectsSortAlphabeticallyIndex = 0;
  14. static const NSInteger kFLEXLiveObjectsSortByCountIndex = 1;
  15. @interface FLEXLiveObjectsTableViewController () <UISearchBarDelegate>
  16. @property (nonatomic, strong) NSDictionary *instanceCountsForClassNames;
  17. @property (nonatomic, readonly) NSArray *allClassNames;
  18. @property (nonatomic, strong) NSArray *filteredClassNames;
  19. @property (nonatomic, strong) UISearchBar *searchBar;
  20. @end
  21. @implementation FLEXLiveObjectsTableViewController
  22. - (void)viewDidLoad
  23. {
  24. [super viewDidLoad];
  25. self.searchBar = [[UISearchBar alloc] init];
  26. self.searchBar.placeholder = [FLEXUtility searchBarPlaceholderText];
  27. self.searchBar.delegate = self;
  28. self.searchBar.showsScopeBar = YES;
  29. self.searchBar.scopeButtonTitles = @[@"Sort Alphabetically", @"Sort by Count"];
  30. [self.searchBar sizeToFit];
  31. self.tableView.tableHeaderView = self.searchBar;
  32. self.refreshControl = [[UIRefreshControl alloc] init];
  33. [self.refreshControl addTarget:self action:@selector(refreshControlDidRefresh:) forControlEvents:UIControlEventValueChanged];
  34. [self reloadTableData];
  35. }
  36. - (NSArray *)allClassNames
  37. {
  38. return [self.instanceCountsForClassNames allKeys];
  39. }
  40. - (void)reloadTableData
  41. {
  42. // Set up a CFMutableDictionary with class pointer keys and NSUInteger values.
  43. // We abuse CFMutableDictionary a little to have primitive keys through judicious casting, but it gets the job done.
  44. // The dictionary is intialized with a 0 count for each class so that it doesn't have to expand during enumeration.
  45. // While it might be a little cleaner to populate an NSMutableDictionary with class name string keys to NSNumber counts,
  46. // we choose the CF/primitives approach because it lets us enumerate the objects in the heap without allocating any memory during enumeration.
  47. // The alternative of creating one NSString/NSNumber per object on the heap ends up polluting the count of live objects quite a bit.
  48. unsigned int classCount = 0;
  49. Class *classes = objc_copyClassList(&classCount);
  50. CFMutableDictionaryRef mutableCountsForClasses = CFDictionaryCreateMutable(NULL, classCount, NULL, NULL);
  51. for (unsigned int i = 0; i < classCount; i++) {
  52. CFDictionarySetValue(mutableCountsForClasses, (__bridge const void *)classes[i], (const void *)0);
  53. }
  54. // Enumerate all objects on the heap to build the counts of instances for each class.
  55. [FLEXHeapEnumerator enumerateLiveObjectsUsingBlock:^(__unsafe_unretained id object, __unsafe_unretained Class actualClass) {
  56. NSUInteger instanceCount = (NSUInteger)CFDictionaryGetValue(mutableCountsForClasses, (__bridge const void *)actualClass);
  57. instanceCount++;
  58. CFDictionarySetValue(mutableCountsForClasses, (__bridge const void *)actualClass, (const void *)instanceCount);
  59. }];
  60. // Convert our CF primitive dictionary into a nicer mapping of class name strings to counts that we will use as the table's model.
  61. NSMutableDictionary *mutableCountsForClassNames = [NSMutableDictionary dictionary];
  62. for (unsigned int i = 0; i < classCount; i++) {
  63. Class class = classes[i];
  64. NSUInteger instanceCount = (NSUInteger)CFDictionaryGetValue(mutableCountsForClasses, (__bridge const void *)(class));
  65. if (instanceCount > 0) {
  66. NSString *className = @(class_getName(class));
  67. [mutableCountsForClassNames setObject:@(instanceCount) forKey:className];
  68. }
  69. }
  70. free(classes);
  71. self.instanceCountsForClassNames = mutableCountsForClassNames;
  72. [self updateTableDataForSearchFilter];
  73. }
  74. - (void)refreshControlDidRefresh:(id)sender
  75. {
  76. [self reloadTableData];
  77. [self.refreshControl endRefreshing];
  78. }
  79. - (void)updateTitle
  80. {
  81. NSString *title = @"Live Objects";
  82. NSUInteger totalCount = 0;
  83. for (NSString *className in self.allClassNames) {
  84. totalCount += [self.instanceCountsForClassNames[className] unsignedIntegerValue];
  85. }
  86. NSUInteger filteredCount = 0;
  87. for (NSString *className in self.filteredClassNames) {
  88. filteredCount += [self.instanceCountsForClassNames[className] unsignedIntegerValue];
  89. }
  90. if (filteredCount == totalCount) {
  91. // Unfiltered
  92. title = [title stringByAppendingFormat:@" (%lu)", (unsigned long)totalCount];
  93. } else {
  94. title = [title stringByAppendingFormat:@" (filtered, %lu)", (unsigned long)filteredCount];
  95. }
  96. self.title = title;
  97. }
  98. #pragma mark - Search
  99. - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
  100. {
  101. [self updateTableDataForSearchFilter];
  102. }
  103. - (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
  104. {
  105. [searchBar resignFirstResponder];
  106. }
  107. - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope
  108. {
  109. [self updateTableDataForSearchFilter];
  110. }
  111. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  112. {
  113. // Dismiss the keyboard when interacting with filtered results.
  114. [self.searchBar endEditing:YES];
  115. }
  116. - (void)updateTableDataForSearchFilter
  117. {
  118. if ([self.searchBar.text length] > 0) {
  119. NSPredicate *searchPreidcate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[cd] %@", self.searchBar.text];
  120. self.filteredClassNames = [self.allClassNames filteredArrayUsingPredicate:searchPreidcate];
  121. } else {
  122. self.filteredClassNames = self.allClassNames;
  123. }
  124. if (self.searchBar.selectedScopeButtonIndex == kFLEXLiveObjectsSortAlphabeticallyIndex) {
  125. self.filteredClassNames = [self.filteredClassNames sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
  126. } else if (self.searchBar.selectedScopeButtonIndex == kFLEXLiveObjectsSortByCountIndex) {
  127. self.filteredClassNames = [self.filteredClassNames sortedArrayUsingComparator:^NSComparisonResult(NSString *className1, NSString *className2) {
  128. NSNumber *count1 = self.instanceCountsForClassNames[className1];
  129. NSNumber *count2 = self.instanceCountsForClassNames[className2];
  130. // Reversed for descending counts.
  131. return [count2 compare:count1];
  132. }];
  133. }
  134. [self updateTitle];
  135. [self.tableView reloadData];
  136. }
  137. #pragma mark - Table view data source
  138. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  139. {
  140. return 1;
  141. }
  142. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  143. {
  144. return [self.filteredClassNames count];
  145. }
  146. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. static NSString *CellIdentifier = @"Cell";
  149. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  150. if (!cell) {
  151. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
  152. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  153. cell.textLabel.font = [FLEXUtility defaultTableViewCellLabelFont];
  154. }
  155. NSString *className = self.filteredClassNames[indexPath.row];
  156. NSNumber *count = self.instanceCountsForClassNames[className];
  157. cell.textLabel.text = [NSString stringWithFormat:@"%@ (%ld)", className, (long)[count integerValue]];
  158. return cell;
  159. }
  160. #pragma mark - Table view delegate
  161. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  162. {
  163. NSString *className = self.filteredClassNames[indexPath.row];
  164. FLEXInstancesTableViewController *instancesViewController = [FLEXInstancesTableViewController instancesTableViewControllerForClassName:className];
  165. [self.navigationController pushViewController:instancesViewController animated:YES];
  166. }
  167. @end