title: NSSortdescriptor ineffective on fetch result from NSManagedContext
body: <p>I'm trying to sort my NSFetchRequest result using a NSSortdescriptor using a key pointing to a NSDate value. My fetch results come out totally random for no clear reason.</p>
  
  <p>The NSManagedObjectContext I'm using is updated with a save from a nested child context created on a subclass of NSOperation. I know all this is done successfully because I can get all the data needed from the parent (main) context. Fetching from it just wont sort on date!</p>
  
  <p>Strange thing is; predicates for selecting the entities (called "Tweet") between two dates works just fine!</p>
  
  <p>Here's some code to illustrate my problem:</p>
  
  <pre><code>NSSortDescriptor* timeDescriptor = [NSSortDescriptor
                                          sortDescriptorWithKey:@"time"
                                          ascending:NO
                                          selector:@selector(compare:)];
  
  NSFetchRequest* request = [NSFetchRequest fetchRequestWithEntityName:@"Tweet"];
  [request setSortDescriptors:[NSArray arrayWithObjects:timeDescriptor, nil]];
  
  NSPredicate* predicate = [NSPredicate predicateWithFormat:@"((time &gt;= %@) AND (time &lt;= %@))",startDate,endDate];
  [request setPredicate:predicate];
  
  NSManagedObjectContext* context = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
  [context setParentContext:[[NSApp delegate] managedObjectContext]];
  [context performBlock:^{
      NSError* error = nil;
      NSArray* results = nil;
  
      results = [context executeFetchRequest:request error:&amp;error];
      // Results here are not ordered correctly
  
      // 2nd try sorting results using fetched array (works!)
      results = [results sortedArrayUsingDescriptors:[NSArray arrayWithObjects:timeDescriptor, nil]];
  
      // This works too but not needed anymore
      /*results = [results sortedArrayUsingComparator:^(id obj1, id obj2) {
          Tweet* tweet1 = (Tweet*)obj1;
          Tweet* tweet2 = (Tweet*)obj2;
          //return [tweet1.time compare:tweet2.time]; // ascending
          return [tweet2.time compare:tweet1.time]; // descending
      }];*/
  
      if ([results count] &gt; 0) {
          for (uint i = 0; i &lt; [results count]; i++) {
              Tweet* tweet = [results objectAtIndex:i];
              NSDate* date = Tweet.time;
              NSLog(@"tweet date: %@", date);
          }
      }
  }];
  </code></pre>
  
  <p>Can anybody tell me why the NSSortDescriptor isn't working for my fetches?</p>
  
  <p>Thanks!</p>
  
  <p>-- Update --</p>
  
  <p>It seems the NSSortDescriptor works fine when I fetch from the main (parent) managedObjectContext on the main thread without using the performBlock method. This still doesn't help me do sorted fetches on a NSPrivateQueueConcurrencyType managedObjectContext. Creating the NSFetchRequest, NSSortDescriptor and NSPredicate inside the performBlock doesn't fix the problem either.</p>