Snowman
Snowman

Reputation: 32091

NSSortDescriptor not being called

I'm trying to execute a fetch request on an entity Folders, and I want a folder named xyz to be the last object when sorted.

  NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription 
                                   entityForName:@"Folder" inManagedObjectContext:appDelegate.managedObjectContext];

    [fetchRequest setEntity:entity];

    NSSortDescriptor *lastDescriptor =
    [[[NSSortDescriptor alloc] initWithKey:@"folderName" ascending:YES comparator:^NSComparisonResult(NSString* name1, NSString* name2) {
        NSLog(@"descriptor");
        if ([name1 isEqualToString:@"xyz"]) {
            return NSOrderedAscending;
        }
        if ([name2 isEqualToString:@"xyz"]) {
            return NSOrderedDescending;
        }

        return [name1 compare:name2];
    }] autorelease];  


    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:lastDescriptor]];
    [fetchRequest setFetchBatchSize:5];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    self.fetchedResultsController = theFetchedResultsController;
    self.fetchedResultsController.delegate=self;   

    [fetchRequest release];
    [theFetchedResultsController release];

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        exit(-1);  // Fail
    }

But none of the NSLog statements are being called. Are they supposed to? And the folder xyz does not appear last, but everything ends up being sorted in alphabetical order. Am I doing something wrong?

Edit: Posted full code

Upvotes: 1

Views: 326

Answers (1)

danh
danh

Reputation: 62686

This code (your code) works:

- (void)sortMe {

    NSDictionary *d0 = [NSDictionary dictionaryWithObject:@"efg" forKey:@"folderName"];
    NSDictionary *d1 = [NSDictionary dictionaryWithObject:@"xyz" forKey:@"folderName"];
    NSDictionary *d2 = [NSDictionary dictionaryWithObject:@"abc" forKey:@"folderName"];
    NSDictionary *d3 = [NSDictionary dictionaryWithObject:@"def" forKey:@"folderName"];
    NSArray *testMe = [NSArray arrayWithObjects:d0, d1, d2, d3, nil];

    NSSortDescriptor *lastDescriptor =
    [[NSSortDescriptor alloc] initWithKey:@"folderName" ascending:YES comparator:^NSComparisonResult(NSString* name1, NSString* name2) {
        NSLog(@"descriptor");
        if ([name1 isEqualToString:@"xyz"]) {
            return NSOrderedAscending;
        }
        if ([name2 isEqualToString:@"xyz"]) {
            return NSOrderedDescending;
        }

        return [name1 compare:name2];
    }];

    NSArray *sorted = [testMe sortedArrayUsingDescriptors:[NSArray arrayWithObject:lastDescriptor]];
    for (NSDictionary *d in sorted) {
        NSLog(@"value=%@", [d valueForKey:@"folderName"]);
    }
}

I predict this code will work, too:

NSError *error;
NSArray *result =  [appDelegate.managedObjectContext executeFetchRequest:fetchRequest error:&error];
// log the array

I think the problem is in the NSFetchedResultsController delegate setup, elsewhere in the code.

Upvotes: 1

Related Questions