如何处理与NSFetchedResultsController的多对多关系? [英] How to deal with many to many relationships with NSFetchedResultsController?
问题描述
确定,所以我有两个实体在我的数据模型(让我们说,entityA和entityB),这两个实体彼此之间有一对多的关系。
OK so I have two entities in my data model (let's say entityA and entityB), both of these entities have a to-many relationship to each other.
我设置了一个NSFetchedResultsController来获取一堆entityA。现在我想让tableview的节名称为entityB的标题。
I have setup a NSFetchedResultsController to fetch a bunch of entityA. Now I'm trying to have the section names for the tableview be the title of entityB.
sectionNameKeyPath:@"entityB.title"
现在这会导致一个问题,其中从该关系返回的节名称显示为title1})或({title1,title2 ... titleN})显然取决于涉及多少不同的entityB。这在一个tableview看起来不是很好,并不按照我想要的对象分组。
Now this causes a problem, where by the section name returned from that relationship appears to be ({title1}) or ({title1,title2...titleN}) obviously depending on how many different entityB's are involved. This doesn't look great in a tableview and doesn't group the objects as I would like.
我想要的是每个entityB标题的一个部分,其中entityA出现在每个部分下,如果需要,可以包含多个部分。我有点失落,因为我应该实现这一点,无论我需要更新谓词以使实体出现多次,还是我需要更新段和头功能做一些处理,因为控制器循环通过对象。
What I would like is a section per entityB title with entityA appearing under each section, under multiple sections if necessary. I'm at a loss as how I am supposed to achieve this whether I need to update the predicate to get the entity to appear multiple times or whether I need to update the section and header functions to do some processing as the controller loops through the objects.
任何帮助:)
感谢
推荐答案
你会得到,因为调用@entityB.title将返回一个数组|标题集。这是你看到的,因为它正在翻译成:
You will get that because the call @"entityB.title" is going to return an array|set of titles. That is what you are seeing as it is translating into:
NSSet *titles = [myEntityA valueForKeyPath@"entityB.title"];
要解决这个问题,您需要反转NSFetchedResultsController,以便通过 EntityB对象。然后你的sectionKeyPath(和你的主要排序)将只对@标题。
To fix this, you need to reverse your NSFetchedResultsController so that you are fetching EntityA objects through EntityB objects. Then your sectionKeyPath (and your primary sort) would be against just @"title".
这篇关于如何处理与NSFetchedResultsController的多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!