title: CoreData Fetch Crashes if used with dispatch_async
body: <p>I have a core-data app that runs without crashing if I perform a fetch inside <code>viewDidLoad</code> like this:</p>
  
  <pre><code>- (void) performCoreDataFetch {
     NSError *error;
      if (![[self fetchedResultsController] performFetch:&amp;error]) {
          exit(-1);  // Fail
      }
   }
  
  - (void)viewDidLoad {
      [super viewDidLoad];
      [self performCoreDataFetch];
  }
  </code></pre>
  
  <p>The only problem with the above way of performing fetch is if the data to be returned is big, it freezes the app for a few seconds (but does return correct result without crashing every single time), so to avoid that I decided to use dispatch_async (code shown below) and call <code>[self performCoreDataFetch]</code> inside it.</p>
  
  <p>But if I run this same [self performCoreDataFetch] inside dispatch_sync <strong>within viewDidLoad</strong>, like shown below,:</p>
  
  <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
          [self performCoreDataFetch];
          dispatch_async(dispatch_get_main_queue(), ^{
             [self.tableView reloadData];
          });
      });
  </code></pre>
  
  <p>Calling <code>[self performCoreDataFetch]</code> within dispatch_async crashes the app randomly saying "<code>-[NSFetchRequest fetchLimit]: message sent to deallocated instance</code>"</p>
  
  <p>My fetchedResultsController method looks like this:</p>
  
  <pre><code>- (NSFetchedResultsController *)fetchedResultsController {
      if (fetchedResultsController != nil) {
          return  fetchedResultsController;
      }
  
      // Create and configure a fetch request with the Organization entity
      NSFetchRequest *request = [[NSFetchRequest alloc] init];
      request.entity = [NSEntityDescription entityForName:@"Organization" inManagedObjectContext:managedObjectContext];
      request.fetchBatchSize = 20;
  
      // create sortDescriptor array
      NSSortDescriptor *nameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];
      NSArray *sortDescriptorArray = [NSArray arrayWithObjects:nameDescriptor, nil];
      request.sortDescriptors = sortDescriptorArray;
  
      NSPredicate *predicate = nil;
  
      predicate = [NSPredicate predicateWithFormat:@"state LIKE %@", filterByState];
      [request setPredicate:predicate];
  
      // Create and initialize the fetchedResultsController
      NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc ] initWithFetchRequest:request managedObjectContext:managedObjectContext sectionNameKeyPath:@"uppercaseFirstLetterOfName" cacheName:nil];
  
      self.fetchedResultsController = aFetchedResultsController;
      fetchedResultsController.delegate = self;
  
      // Memory management
      filterByState = nil;
  //    [sortDescriptorArray release];
      [nameDescriptor release];
  //    [predicate release];
      [request release];
      [aFetchedResultsController release];
  
      return fetchedResultsController;
  
  }
  </code></pre>