title: Perform fetch without freezing the UI
body: <p>I'm making a small chat app. In my app I'm using NSFetchedResultsController. There are 2 tableViews, 1 for lobby and 1 for the chat room. The problem is that whenever I enter a chat room I have to wait for NSFetchedResultsController to perform fetch and load all the data before I can start typing anything. I was wondering if it's possible to preform fetch in background or somehow let the user start typing before the last messages are loaded.</p>
  
  <p>So if I set that part in viewDidLoad method, my UI just freezes until all the data is loaded:</p>
  
  <pre><code>NSError *_error;
  if (![self.fetchedResultsController performFetch:&amp;_error]) {
      NSLog(@"Unresolved error %@, %@", _error, [_error userInfo]);
  }
  </code></pre>
  
  <p>and if I set that piece of code into a separate method and then call that method in viewDidLoad in background</p>
  
  <pre><code>[self performSelectorInBackground:@selector(fetchIt) withObject:nil];
  </code></pre>
  
  <p>tableView is just not updating, so I have to go back to the first tableView and then come back to get any results.</p>
  
  <p>Thank you.</p>
  
  <p>Any help would be appreciated</p>
  
  <p><strong>update</strong></p>
  
  <p>I did try a few other ways to do it.</p>
  
  <p>Way 1:</p>
  
  <pre><code>-(void)reloadTheTable
  {
    [self.tableView reloadData];
  }
  
  -(void)fetchIt
  {
    NSError *_error;
    if (![self.fetchedResultsController performFetch:&amp;_error]) {
      NSLog(@"Unresolved error %@, %@", _error, [_error userInfo]);
    }
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
    [self performSelectorOnMainThread:@selector(reloadTheTable) withObject:nil waitUntilDone:NO];
    [pool release];
  }
  
  - (void)viewDidLoad {
    //some code
    [self performSelectorInBackground:@selector(fetchIt) withObject:nil];
    //some other code
  }
  </code></pre>
  
  <p>result: tableView doesn't show any data, need to re-open that view controller</p>
  
  <p>Way 2:</p>
  
  <pre><code>- (void)viewDidLoad {
    //some code
    dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
      NSError *_error;
      if (![self.fetchedResultsController performFetch:&amp;_error]) {
          NSLog(@"Unresolved error %@, %@", _error, [_error userInfo]);
      }
    });
    //some other code
  }
  </code></pre>
  
  <p>result: tableView updates, but UI freezes</p>
  
  <p>Way 3:</p>
  
  <pre><code>-(void)fetchIt
  {
    NSError *_error;
    if (![self.fetchedResultsController performFetch:&amp;_error]) {
      NSLog(@"Unresolved error %@, %@", _error, [_error userInfo]);
    }
  }
  
  - (void)viewDidLoad {
    //some code
    [self performSelectorOnMainThread:@selector(fetchIt) withObject:nil waitUntilDone:NO];
    //some other code
  }
  </code></pre>
  
  <p>result: UI freezes, table view updates.</p>
  
  <p>As I read somewhere, everything that has to do something with UI must be done on a main thread. But I still can't figure out how to do it without freezing the tableView.</p>
  
  <p>Thank you for reading.</p>
  
  <p>Any help is appreciated</p>