0

Given the following core data model:

EntityA <--->> EntityB <--->> EntityC

I already have fetched the required EntityA in a first TableViewController. When selecting a row, the user goes to a second ViewController, following the relationship. This second view has a @property EntityB *entityB. In this second view, there are controls that will modify attributes of EntityC. So if EntityB has 4 EntityC, there will be 4 sliders. Each slider modify an attribute of one of the EntityC.

My question is: what's the best way to select the right EntityC? The following predicate

NSEntityDescription *entity = [NSEntityDescription entityForName:@"EntityC" inManagedObjectContext:self.managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entityB == %@ AND rank == <#some number#>", self.entityB];

returns the correct value. But isn't this overkill when selecting 1 out of 4 entities?

SahilS
  • 396
  • 5
  • 7
invalidArgument
  • 2,289
  • 4
  • 24
  • 35
  • Surely you already have them relationship contents to set the initial slider value? – Wain Nov 19 '13 at 22:05
  • Very good question. But in this case, I need initial values to always be default values (50 on a 0 to 100 scale). – invalidArgument Nov 19 '13 at 22:07
  • Is the relationship ordered? – Wain Nov 19 '13 at 22:22
  • No... Can it be ordered according to an attribute of the target entity? (From EntityB, EntityC are ordered based on a attribute of EntityC?) – invalidArgument Nov 19 '13 at 22:28
  • The order is set when you create / add to the relationship (so you can choose). – Wain Nov 19 '13 at 22:47
  • What do you mean by 'select the right EntityC' ? Can't you simply associate the items in the entityB.children (NSSet) with the controls themselves such that any movement of the slider will automatically update the associated entityC – Duncan Groenewald Nov 20 '13 at 00:39
  • @DuncanGroenewald: how would you associate entityB.children with the control? And what I mean is entityC with rank x should go with slider x. So when the slider x is set to a new value, the corresponding EntityC is updated. – invalidArgument Nov 20 '13 at 01:54

2 Answers2

0

If you use an ordered relationship then you can just get the appropriate object directly. If not then you need to use a predicate (though you could run it on the relationship (if small count) or the MOC (if large count).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I will implement this solution and let you know. Thanks! – invalidArgument Nov 20 '13 at 02:05
  • Your answer did work. So I will accept it. But FYI, I ran into [this issue](http://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors). The workaround worked, but I may refactor and try something else... – invalidArgument Nov 20 '13 at 05:03
  • [That issue](http://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors) is one of the reasons for my recommendation. – Mundi Nov 20 '13 at 14:50
0

I have had bad experience with ordered relationships. Rearranging is error prone, and you do not have many of the advantages of either NSSet or NSArray because NSOrderedSet is derived from neither.

Instead, add a position attribute to EntityC. This seems to me the most straight-forward way to do it. And no, IMO this is not overkill even with a handful of instances. Now you can sort and identify the relationship entities quite easily.

Mundi
  • 79,884
  • 17
  • 117
  • 140