Re: [iOS] Strange behavior of NSFetchedResultsController
Re: [iOS] Strange behavior of NSFetchedResultsController
- Subject: Re: [iOS] Strange behavior of NSFetchedResultsController
- From: WT <email@hidden>
- Date: Thu, 10 Feb 2011 14:27:56 -0200
The example of countries is just that, an example. The actual app is about something else. Also, the color of the text is not a model property - the selected status is.
I think you didn't quite understand what the problem I'm having is. The problem is that NSFetchedResultsController is ignoring a transformable attribute used as a sorting key.
On Feb 10, 2011, at 2:10 PM, claw wrote:
> hi,
>
> i want you to propose just an idea , a remark, ....on apple documentation is said
>
> The NSFetchRequest class is used to describe search criteria used to retrieve data from a persistent store..... implicitely you must record all of them and update them sometimes....
>
> in your idea there is a lot of different kind of countries ( india, honduras ....) which can have a common property: a selected color that can vary a lot of times....
>
> why not trying to use an Hash Table in order to use weak reference with NSHashTable ??....instead of each time ask core data to update all the entire store....
>
> best regards
>
>
>
>
> Le 10 févr. 2011 à 16:19, WT a écrit :
>
>> 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@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
>
_______________________________________________
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