one to many relationship

I am trying to get my head around a problem involving a one to many relationship. I'm not sure that my approach to this design is a good one.


I have two entities, address and rooms. An address can have many rooms. I have set up a one to many relationship between the address table and room table. The problem is differentiating the "Living Room" of address 1 from the "Living Room" of address 2. Currently when I call NSManagedObject, the table rows are displaying all the rooms regardless of the address being chosen. I am not sure how to uniquely identify a room with an address.


Sorry this is so vague, but I'm pretty new to core data, and I'm not even sure if my approach is a valid one. Any help to get me pointe in the right direction would be greatly appreciated.

Typically you have the rooms specified to contain a to-one relationship address which is the inverse of that to-many relationship rooms.

That's the simplest way of differentiating the living room of addressX from the living room of address Y.


And that lets you do all sorts of convenient things like select an address from your list of addresses; pass that reference to another table view controller, and have it specify a predicate for your fetch request so that it filters to just that address:


    // Given address as the user selected Address instance     NSPredicate *predicate = [NSPredicate predicateWithFormat @"address = %@", address];

You should include a 'room' attribute or a 'roomType' attribute inside of your Address entity. Then try

NSString *roomName = _yourRoomName; //

NSPredicate *predicate = [NSPredicate predicateWithFormat @"roomType = %K", _yourRoomName];


Please let me know if that helped you solve your problem. Thank you.

one to many relationship
 
 
Q