Skip to content

Conversation

@lukestringer90
Copy link

I was using SJOSearchableFetchedResultsController in a scenario where I wanted to display a section index for a table view. So I made some changes to facilitate this. Mainly the ability to override a method in your subclass to specify the sectionKeyPath that should be used when constructed new NSFetchedResultsControllers.

This worked fine but then I released that the table view section index should not be displayed when the user is searching. So the newFetchedResultsControllerWithSearch: method takes this into account by ignoring any specified sectionKeyPath if the user is searching. This necessitated the searchIsActive property, which is set to YES when searching begins, and NO when searching is cancelled.

I also added the ability to construct a SJOSearchableFetchedResultsController with a managed object context if you want to use SJOSearchableFetchedResultsController without being depending on an SJOStore instance.

So to use a section index in a SJOSearchableFetchedResultsController subclass:

- (NSString *)sectionKeyPathForSearchableFetchedResultsController:(SJOSearchableFetchedResultsController *)controller {
    return @"uppercaseFirstLetterTitle"; // the sectionKeyPath
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    // No section indexes if searching
    if (self.searchIsActive) {
        return nil;
    }
    return self.sectionIndexes;
}

 (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
    return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
               atIndex:(NSInteger)index
{
    return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

Luke Stringer added 5 commits April 18, 2014 22:52
…ed object context.

This allows a SJOSearchableFetchedResultsController to be constructed without
the dependency on SJOStore. This is helpful if you only want to use this
class without the the rest of SJODataKit, say with your own Core Data
framework, or simply using standard Core Data classes.

If the `store` property is set then the `managedObjectContext` property
is set to be the `store.mainContext` managed object context.

I think that another constructed should also be offered now, and the `store`
property made readonly publicly. This way the `managedObjectContext` property
would be immutable which would better as you would't want this changing
after the object's construction.
This is helpful if you have a search-specific view setup. Say for example
a tab bar controller is used. If a view controller is popped from the
navigation stack to SJOSearchableFetchedResultsController which is
actively searching, then the tab bar controller may want hiding. The
`searchIsActive` property enables this behaviour.
…ex is to be displayed for the tableView.

If `searchIsActive` is YES then the sectionKeyPath return value will be
ignored and nil used regardless. This is because A a section index should
not be shown while searching, and B executed fetch requests take longer
when sections are used. When searching this is especially noticable as a
new fetch request is executed upon each key stroke during search.
@blork
Copy link
Owner

blork commented Apr 19, 2014

Great stuff. Might as well remove the Store initialiser and property, since all it needs is the context.
If we're gonna release SQK, maybe we should add this to it and I'll just deprecate SJO?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants