Core Data Relationships

I am working on a project which provides a way to collect data about rooms at a specific address. I have set up a relationship in the data model between the rooms and address tables. The project currently presents a UITableView of addresses which the user is able to drill down via address to another UItableView to view rooms. This is where I am clueless. If there are no rooms, a custom UIView is presented to enter the room data. The user enters the data in the UIView for each data point and then presses save. The IBAction for the save button currently sets the value for each key in the related table. I am assuming it is here that I need to establish the relationship between an address and its rooms, but I cannot find the proper way to do this. I have tried NSSet setWithObject in the setValue method:


[newRoom setValue:[NSSet setWithObject:_address] roomName.text forKey:@"roomName"];


but Xcode wants me to insert a colon between roomName and text.


Ultimately I want the user to be able to choose an address and manage room data for that address, but I am unsure how to establish the relationship between the address and its rooms.


any help would be greatly appreciated.

[newRoom setValue:[NSSet setWithObject:_address] roomName.text forKey:@"roomName"];

is wrong. I don't know what you're expecting the [NSSet setWithObject:_address] stuck in the middle is for.

What you're looking should look like

[newRoom setValue: roomName.text forKey: @"roomName"];

or you should use the CoreData generated setter

[newRoom setRoomName: roomName.text];

I am currenly using


[room setValue:roomName.text forKey:@"roomName"];


Sticking _address in the middle was suggestedd elsewhere.


My data model has a to one relationship between addresses and rooms tables as you pointed out in an earlier post. But I am stuck on how to establish the relationship between an address and its rooms. In other words when the user enters an adress on the address form, and then proceeds to enter rooms on the room form, how do I establish that specific relationship in code.


I understand it conceptually and could handle it with stratight SQL, but core data has me confused.


Thanks for your reply.

You establish a relationship between two entities, an Address entity and a Room entity, by doing the following things:

  1. Get a reference to the address
  2. Get a reference to the room
  3. Calling [room setValue: address forKey: @"name of address relationship"];


As the user enters an address on the address form, you're going to be performing fetches to locate matching addresses. You're probably going to want to display those addresses in a picker, table view, or collection view. Once the user has a selected address, you'll have the address reference.


I assume your purpose for entering the rooms is that you're going to be creating and/or editing room instances for the user.


I'd say, after working with CoreData and sqlite on different projects, there's an even chance that trying to think about how you'd do this in straight SQL is going to confuse you. It's all objects all the way down, you need to think about creating objects, setting properties, and allowing the user to select objects out of fetch results.

Core Data Relationships
 
 
Q