0

I am currently facing a (to me) strange issue as follows:

I have a child MOC (NSManagedObjectContext) with about two dozen MOs (NSManagedObjects). Each MO has an optional Boolean attribute flag. All of these are set to @(NO). fetchedResultsController is an NSFetchedResultsController which fetches these MOs, whose managedObjectContext is moc and whose sectionNameKeyPath is @"flag". fetchedResultsController also has an sort descriptor on flag (and another secondary sort attribute).

After running these lines of code

NSAssert(!moc.hasChanges, nil); // no unsaved changes
BOOL flag = [fetchedResultsController performFetch: &error];
NSAssert(flag && (error == nil), nil); // no errors

I observe the following:

  1. fetchedResultsController.fetchedResults contains as many MOs as does moc and all their flags are @(NO) (as one would expect).
  2. fetchedResultsController.sections.count and fetchedResultsController.sectionIndexTitles.count are both 1 (as one would expect, since all flags have the same value).
  3. fetchedResultsController.sectionIndexTitles[0] is @"1".

The third item appears wrong to me. I would have expected @"0" (since this is the capitalized first letter of [@(NO) description]).

What could be wrong here and how can I obtain the right section index title in this case?

UPDATE I now looks as if the problem may be (still) related to employing flag (an optional Boolean Core Data attribute) as sectionNameKeyPath. Even if values of flag differ, performFetch: leads to only one section titled @"1".

Community
  • 1
  • 1
Drux
  • 11,992
  • 13
  • 66
  • 116

1 Answers1

1

I think it is not reliable to rely on the description implementation of the section title field. This might be appropriate for a string but it seems counter-intuitive that this is the intended solution for numeric values. You probably do not want these values in your final implementation anyway.

You should implement sectionIndexTitleForSectionName, check the boolean value there and return a proper string. Note that this method already receives a string, so you need to check if it is the right one. I suspect it is not, and you have to make sure that the NSNumber generates the correct string. Maybe you need to add a method to NSNumber as a category to return the proper string for the boolean value. Your sectionNameKeyPath would then be something like "flag.toString" or similar.

Another thing you might want to check: depending on how you set up your managed object subclasses and your model, you might have forgotten to de-reference the value of the NSNumber.

BOOL falseValue = @(NO).boolValue; // NO

but

BOOL falseValue = @(NO);           // YES
Mundi
  • 79,884
  • 17
  • 117
  • 140
  • I think you got it almost right: implementing `sectionIndexTitleForSectionName` does not help, because it receives already a derived `NSString`, no longer the Boolean. I was indeed mistaken in assuming that the derivation was done with `description`. What solves the problem is defining a category on `NSString` with a method say, `toString` for deriving `NSString`s from `NSNumber`s , and control the derivation with `sectionNameKeyPath`set to `flag.toString`. Do you want to update you answer accordingly? If so, I shall accept, because the current version put me on the right track. – Drux Feb 18 '15 at 13:57
  • OK, I updated my answer. It seems to me that you need a category on `NSNumber`, though, rather than on `NSString`. – Mundi Feb 18 '15 at 16:19
  • Yep, `NSNumber` (typo). – Drux Feb 18 '15 at 18:41