Re: [iOS] Strange behavior of NSFetchedResultsController
Re: [iOS] Strange behavior of NSFetchedResultsController
- Subject: Re: [iOS] Strange behavior of NSFetchedResultsController
- From: Quincey Morris <email@hidden>
- Date: Thu, 10 Feb 2011 09:54:05 -0800
On Feb 10, 2011, at 07:19, WT wrote:
> 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.
What you've shown above, assuming it's actual code from your managed object subclass, is not a transformable attribute but a derived attribute -- one that depends for its value solely on the values of other properties, and doesn't make use of any backing storage of its own. A transformable attribute is one whose backing storage representation is a different class from its in memory representation, with the translation between the two forms handled automatically by a value transformer.
Perhaps you're assuming that Core Data will internally call the above getter to figure out what to put into the store. It doesn't work that way. Most likely your store contains nil values for the "indexOrName" attribute. (I'm assuming your Core Data model actually has an "indexOrName" attribute. If not, it still won't work, but for a different reason.)
The thing you're sorting is a fetched results controller. Presumably that sorts itself based on the fetched attribute values (all nil for "indexOrName" in this case), which means that all objects have the same value for the attribute, so the result of sorting is essentially random, as you've seen.
You either need to get the actual values of "indexOrName" into the store, or you need to do your own sorting after the objects have been fetched.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden