I have an entity Tagwith a string property tagName. I went to get all the objects in this Entity in NSFetchedResultsController, but I want the tag with tagName"Main" to be the first object. Here is what I am doing now:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Tag" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSSortDescriptor *lastDescriptor2 =
[[[NSSortDescriptor alloc] initWithKey:@"tagName" ascending:NO comparator:^NSComparisonResult(NSString* tag1, NSString* tag2) {
NSLog(@"compare");
if ([tag1 isEqualToString:@"main"]) return NSOrderedAscending;
if ([tag2 isEqualToString:@"main"]) return NSOrderedDescending;
return [tag1 compare:tag2];
}] autorelease];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:lastDescriptor2]];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
self.fetchedResultsController.delegate=self;
if (![[self fetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
This code is called in my method viewDidLoadand is called only once. The first time it was called, for some reason, sortDescriptor is not used here - it just is not called (the operator is NSLogalso not displayed). My results are returned solely based on the value BOOLthat I specify for ascending- the block is ignored.
Tag MOC NSFetchedResultsController, sortDescriptor, NSLog(@"compare"), ! , , .
?