Hello list,

a little while ago I posted a question regarding some strange behavior by an 
NSFetchedResultsController (under the same subject line as this message). Since 
then I've narrowed the problem down a bit more and now have a test project to 
show it.

Here's what the goal is, in a nutshell.

I have a list of, say, countries. Some are "selected", others not. I need to 
display all the countries in the same tableview, in the same section, and what 
differentiates the selected ones from the deselected ones is their text color. 
Moreover, the selected ones should be sorted ascending by their "index" (an 
integer that's used to keep track of their position in the list) while the 
deselected ones must be sorted ascending by their "name".

The test project's core data model has a single entity, "CountryCD", with the 
attributes "name", "index", "selected" (representing a boolean), and a 
transformable attribute "indexOrName" which returns the entity's index if it's 
selected or its name if it's deselected.

- (id) indexOrName;
{
    if ([self.selected isEqualToNumber: [NSNumber numberWithBool: YES]])
    {
        return self.index;
    }
    else
    {
        return self.name;
    }
}

The fetched results controller is defined as usual but contains two sort 
descriptors:

NSSortDescriptor* sortBySelected = [[NSSortDescriptor alloc]
    initWithKey: @"selected" ascending: NO];

NSSortDescriptor* sortByIndexOrName = [[NSSortDescriptor alloc]
    initWithKey: @"indexOrName" ascending: YES];

the idea being that entities get sorted first by their "selected" status and 
then by their "indexOrName" attribute. Since that attribute returns the 
entity's "index" or its "name", depending on the entity's "selected" status, 
the fetched results controller *should* sort the entities as desired.

Should, but doesn't. And that's the problem I can't seem to solve.

In the test project, I create country entities in the following order

[self createCountryOfName: @"India"];
[self createCountryOfName: @"Honduras"];
[self createCountryOfName: @"Germany"];
[self createCountryOfName: @"Denmark"];
[self createCountryOfName: @"Brazil"];
[self createCountryOfName: @"Egypt"];
[self createCountryOfName: @"Australia"];
[self createCountryOfName: @"China"];
[self createCountryOfName: @"Finland"];

and make every second one deselected, which *should* result in them being 
displayed as follows:

// (ordered by index)
// India
// Germany
// Brazil
// Australia
// Finland

// (ordered by name)
// China
// Denmark
// Egypt
// Honduras

Alas, they're displayed like this (or some other seemingly random order):

(selected)
Brazil
Germany
Australia
India
Finland

(deselected)
Honduras
Denmark
Egypt
China

Neither the selected ones are sorted by their "index" attribute nor the 
deselected ones are sorted by their "name" attribute. In fact, the test project 
shows that "indexOrName" is not accessed at all when the fetched results 
controller does its fetch.

First I thought that there might be something wrong with the way I implemented 
the fetched results controller, but if you replace "indexOrName" in the sort 
descriptor with either "index" or "name", it works as would be expected.

So then I thought that using a transformable attribute is not the way to go in 
order to achieve the result I need to achieve, but I can't see any other way to 
do it.

If using a transformable attribute isn't the way to go, can anyone suggest an 
alternative? If using a transformable attribute should work, then why doesn't 
it as I've implemented it? It appears to me that NSFetchedResultsController 
ignores a sort descriptor based on a transformable attribute.

The test project can be found here:
http://www.restlessbrain.com/FRC_Test.zip

I'd greatly appreciate any help. I've already spent several days trying to 
figure this out but got nowhere and ran out of ideas.

Thanks in advance.
WT

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to