Getting NSFetchedResultsController, NSSortDescription and sectionNameForKeyPath to work together

后端 未结 4 1757
陌清茗
陌清茗 2021-01-18 02:23

I\'m currently working on an App that has a couple of Entities and relationships as illustrated below:

Item <<--> Category.

相关标签:
4条回答
  • 2021-01-18 02:46

    You probably need to subclass NSFetchedResultsController and customize the section name functions. See the NSFetchedResultsController class docs on subclassing.

    0 讨论(0)
  • 2021-01-18 02:47

    That's a perfectly good solution to the problem, Rog.

    You certainly don't want/need to subclass NSFetchedResultsController.

    @aroth, we don't have enough information to know the details of his object model, but the names certainly imply that category does not own item. Item has a category. His intent is to display a list of items, that is why he is fetching items.

    0 讨论(0)
  • 2021-01-18 02:50

    Why are you fetching Item and not Category? If I understand your relationships correctly, Category owns a 1 to many relationship with Item, so in theory a Category instance should have an 'items' property that returns every Item in that category.

    If that's the case, then you could simply fetch all of your categories, and then sort them by displayOrder. Then, forget about using sections in the NSFetchedResultsController itself. Instead, your associated tableView methods would look something like:

    - (NSInteger)numberOfSections {
        return [[self.fetchedResultsController fetchedObjects] count];
    }
    
    - (NSInteger)numberOfRowsInSection:(NSInteger)section {
        Category* category = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
        return [[category items] count];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
        Category* category = [[self.fetchedResultsController fetchedObjects] objectAtIndex:section];
        return category.name;
    } 
    

    In short, I think you are overcomplicating things by fetching Item instead of Category, and by trying to make the NSFetchedResultsController manage your section grouping for you. It is much simpler and requires much less code to just do the section management yourself.

    0 讨论(0)
  • 2021-01-18 03:09

    As far as sorting goes, the documentation has this to say:

    If the controller generates sections, the first sort descriptor in the array is used to group the objects into sections; its key must either be the same as sectionNameKeyPath or the relative ordering using its key must match that using sectionNameKeyPath.

    In English (you may already know this Rog, but then again you may not and certainly people who search this later may appreciate the explanation), that means if you're using sections then the sorting on the NSFetchRequest must group all items in the same section together. This could be by making the first sort criteria be the field used as the section name, or it could be by making the first sort criteria be something else that results in the same sort of grouping.

    The documentation doesn't specify what happens if you screw this up; it's possible it would just totally screw up the section names, repeat sections, skip sections, detect the situation and "fix" your sorting, or even just crash. Do any of your categories have the same displayOrder?

    Your solution is certainly workable, and if you can't get it to work correctly sorting by displayOrder while titling sections by category.name it's probably your best solution.

    0 讨论(0)
提交回复
热议问题