开发者

sectionNameKeyPath through multiple relationships

开发者 https://www.devze.com 2023-01-16 17:47 出处:网络
I am working on an app that uses Core Data and an NSFetchedResultsController.The model setup is like this:

I am working on an app that uses Core Data and an NSFetchedResultsController. The model setup is like this:

/-----------\          /-----------\          /-----------\
|Part       |          |Kit        |          |Source     |
|-----------|          |-----------|          |-----------|
|name       |          |name       |          |name       |
|dimensions |          |description|          |location   |
|...        |          |...        |          |...        |
|-----------|          |-----------|          |-----------|
|kits       | <<-\     |source     | <<-----1 |kits       |
|           |     \->> |parts      |          |           |
\-----------/          \-----------/          \-----------/

So, a Part has a many-to-any relationship with a Kit (a part can b开发者_开发问答e in one or more kits, and a kit can contain one or more parts). And each Kit comes from a Source which may provide one or more Kits.

I am currently creating a UITableView that uses an NSFetchedResultsController with a simple listing of all of the Part objects. I would like to group the parts into sections, with each section being a Source's name. I.e.:

|-------------------------------|
|Source One                     |
|-------------------------------|
|Part One                       |
|Part Two                       |
|Part Three                     |
|-------------------------------|
|Source Two                     |
|-------------------------------|
|Part Four                      |
|Part Two                       |
|Part Five                      |
|....                           |
|-------------------------------|
|             O                 |
|-------------------------------|

Since a Part may come from multiple Sources, is it possible to do this with an NSFetchedResultsController by using the sectionNameKeyPath, or do I have to do this a completely different way?

Thanks!


I don't think so, because the NSFetchedResultsController won't know which Kit to use to sort Parts by, since it's a many-many relationship.

Think of a tree view, or even Objective-C inheritance structure, where each child node has exactly one parent, but itself can have many children nodes. You can easily recurse up the tree from the child node up to its parent. But if a child could have many parents, it wouldn't intrinsically know which parent to choose when recursing up the "tree" (which is more like a graph now).

So in your data model, does the Parts have to have many Kits? Because if you change the many-many relationship between Kit and Part to a to-many relationship, where a Kit can have many Parts, but a Part can have only one Kit, then you could set the sectionNameKeyPath on NSFetchedResultsController to @"kit.source.name" to sort by name, and everything would work fine.

Of course I don't know exactly how your app works, if you do really need a many-many relationship between Kit and Part, but if you do, then you probably would not be able to use an NSFetchedResultsController, and would have to implement fetching Sources and implementing the UITableViewDataSource methods yourself :(


You can get this result using NSFetchedResultsController for Many to Many relationships

See this answer: How to deal with many to many relationships with NSFetchedResultsController?

And example project: https://github.com/dmathewwws/Many-to-Many-CoreData

0

精彩评论

暂无评论...
验证码 换一张
取 消