0

I am using core data with NSFetchedResultController. In this case, I have an n number of user groups and a user can select a particular group or select all group. So manually I added All group title, static ID and relevant content to the database.

NSFetchRequest <IRSUserAssetGroup *> *fetchRequest = [NSFetchRequest fetchRequestWithEntityName: [IRSUserAssetGroup entityName]];

NSSortDescriptor *nameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey: NSStringFromSelector(@selector(assetGroupName))
                                                                     ascending: YES
                                                                      selector: @selector(localizedCaseInsensitiveCompare:)];

[fetchRequest setSortDescriptors: @[nameSortDescriptor]];

[fetchRequest setFetchBatchSize: 4];

NSManagedObjectContext *mainContext = [[IRSCoreDataManager sharedManager] managedObjectContext];

NSFetchedResultsController <IRSUserAssetGroup *> *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest: fetchRequest
                                                                                                                 managedObjectContext: mainContext
                                                                                                                   sectionNameKeyPath: nil
                                                                                                                            cacheName: nil];

NSError *fetchError = nil;

if (![fetchedResultsController performFetch: &fetchError])
    FCALog(@"fetch error: %@", fetchError);

[self setUserAssetGroupFetchedResultsController: fetchedResultsController];

This is my FRC code. I am using sort by name. Some group name has start with the number. Because of this All group not place in first place.

Is that possible All group place always top on sort?

Extra: I am using table view with search option

Example: @[@1, @5, @"text", @"aaaaaaa", @"All group"]. Here All group show always on top of the list. How can I sort like that.

Output: @[@"All group", @1, @5, @"aaaaaaa", @"text"]

Mathi Arasan
  • 869
  • 2
  • 10
  • 32
  • Don't solve this in the data model, but manually add the static cell in the TableViewDataSource. So the object at index 0 is you `All groups` cell, just remember to subtract 1 from the index when grabbing the item from the `FetchedresultsController`. – rckoenes Jun 07 '17 at 11:41
  • Thanks. Now, I am working kind of sort only. What do you think about the @jonRose answer? Which one is good, like efficient, speed, etc., – Mathi Arasan Jun 07 '17 at 12:10
  • As I mention before there is static `ID` for `All group`, Now I added and predicate `NSPredicate *allGroupPredicate = [NSPredicate predicateWithFormat:@"%K != %d", NSStringFromSelector(@selector(assetGroupID)), allUserGroupID];` with this. On `cellForRowAtIndexPath` Put static cell setup for `All group`. Is that what you try to convey? – Mathi Arasan Jun 07 '17 at 12:28

1 Answers1

0

You and I know that 'All group' is special because we read and understand the word, but core data has no way of knowing that the 'All group' is special; all it sees it that it starts with an "A". You could add another property to your Entity isAllGroup and first sort by that, and then by assetGroupName.

Or you can have a property 'groupOrder' which is 0 for the "all Group" and 100 for every other groups. This allows you to have more room to have other special group ordering without creating a new property.

An even more general approach would be to add a property assetGroupNameForSort and sort by that instead of assetGroupName which is used for display only. For most group the values are identical but for the "All Group" it is "__All Group" or something like that. This can also be used fix issue that can happen with names that are in difference languages that are sorted in ways you don't expect.

I would recommend the first approach, just add an 'isAllGroup' property. It is the simplest and the data is readily understandable.

Also: minor issue - a fetchedResultsController does not respect FetchBatchSize so you can remove it.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36
  • Thanks for pointing out the minor issue. Can you explain why? Because `FetchBatchSize` also part of fetch result means `NSFetchRequest`. – Mathi Arasan Jun 07 '17 at 12:14
  • I don't want to update (or versioning) database for this minor change. So I am going with added another `AND` predicate `NSPredicate *allGroupPredicate = [NSPredicate predicateWithFormat:@"%K != %d", NSStringFromSelector(@selector(assetGroupID)), allUserGroupID];` with this. On `cellForRowAtIndexPath` Put static cell setup for `All group`. And once again thanks for more option to sort. – Mathi Arasan Jun 07 '17 at 12:31
  • if a fetchedResultsController respected batchSize because apple did not write it to respect batchSize. I would guess that they did this because it would add a huge amount of complexity to the code. For example if there was an insert it would also cause a delete (because it went over the batchSize). – Jon Rose Jun 07 '17 at 12:52
  • I don't think you should be afraid of a version model. I do them all the time even for very minor changes. – Jon Rose Jun 07 '17 at 12:53
  • Again thanks for an explanation. Previously I didn't try version model. That's why. Okay, let me give a shot for version model. (Mind voice) In the feature, Apple may consider this kind of sorting methods. Small sorting thing but few manual works. – Mathi Arasan Jun 08 '17 at 11:06