title: *** Collection <__NSArrayM: 0x117d7320> was mutated while being enumerated.'
body: <p>I made a bug when I implement the search function. I opened an asynchronous thread. But when deleting a character (a digit of a phone number), the app would crash.</p>
  
  <p>Error:</p>
  
  <blockquote>
    <p>*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection &lt;__NSArrayM: 0x117d7320> was mutated while being enumerated.'</p>
  </blockquote>
  
  <p>Code:</p>
  
  <pre><code>- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
      //
      self.isSearch = YES;
  
      //remove the last search all the contacts
      [self.resultArr removeAllObjects];
      //
      [self.rcs_SearchTableView reloadData];
      //
      dispatch_queue_t uploadQueue = dispatch_get_global_queue(0, 0);
      dispatch_queue_t getMainQueue = dispatch_get_main_queue();
      dispatch_async(uploadQueue, ^{
  
          NSMutableArray *phoneArr = (NSMutableArray *)[self rcs_GetPhoneNumberFromeDatabaseWithPhone:searchText];
          //
          //NSLog(@"清空上次搜索的数据:%@", self.resultArr);
          //NSLog(@"输入的关键字是---%@---%lu",searchText,(unsigned long)searchText.length);
          if (0 == searchText.length || [searchText isEqualToString:@" "]) {
              self.isSearch = NO;
              //[self.rcs_SearchTableView reloadData];
              [self.resultArr removeAllObjects];
          }
          //[self.rcs_SearchTableView reloadData];
  
          if (0 != phoneArr.count) {
              //
              for (NSUInteger i = 0; i &lt; phoneArr.count; i ++) {
  
                  RCSPhoneModel *flagPhoneModel = phoneArr[i];
  
  
                  for (NSUInteger i = 0; i &lt; self.rcsRecentSearchDataSource.count; i ++) {
  
                      RCSContactModel *flagModel = self.rcsRecentSearchDataSource[i];
  
                      if ([flagPhoneModel.serverId isEqualToString:flagModel.serverId] || [flagPhoneModel.phone isEqualToString:flagModel.name]) {
  
                          //the same contact has multiple Numbers To prevent repeated add the same contacts
                          if (![self.resultArr containsObject:flagModel]) {
  
                              [self.resultArr addObject:flagModel];
                              continue;
                          }
                      }
                  }
              }
  
          }else{
              //search contacts by name
  
              for (NSInteger i = 0; i &lt; self.rcsRecentSearchDataSource.count; i ++) {
                  RCSContactModel *model = self.rcsRecentSearchDataSource[i];
                  NSString *nameStr = model.name;
                  if (nameStr.length &gt;= searchText.length) {
                      //search all the name
                      if ([nameStr containsString:searchText]) {
                          [self.resultArr addObject:model];
                      }
                  }
              }
          }
          //
          if (self.resultArr.count &gt; 0) {
              self.isSearch = YES;
              //[self.rcs_SearchTableView reloadData];
          }
          //The phone contacts or local contact synchronized to the server
          dispatch_async(getMainQueue, ^{
  
              [self.rcs_SearchTableView reloadData];
          });
      });
  }
  </code></pre>