Retrieving data in a Core Data Relationship - ios

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.

It sounds like the Address to Room relationship is one to many: each Address can have many Rooms, whereas each Room relates to only one Address. So the Address entity should have a rooms property to represent the relationship, and the Room entity should likewise have an address property. (If you have used different names for these properties, amend below where necessary).
When the user selects a row in the table view of addresses, you determine the corresponding Address object. Let's call that chosenAddress. Your second table view should then display only those Room objects that relate to the chosenAddress. When you create a new Room object, let's call it newRoom, you can establish the relationship to chosenAddress with:
[newRoom setValue:chosenAddress forKey:#"address"];
or, if you have created NSManagedObject subclasses for Address and Room:
newRoom.address = chosenAddress;
Note that CoreData will automatically set the inverse relationship, adding newRoom to the set given by chosenAddress.rooms.

Related

Adding properties to many to many relationship?

I have been using Core Data to model my database. I have 2 entities that are in many to many relationship. Each person can have many addresses, and on each address multiple persons can live.
Now i would like to add property to this relationship. For example one person - address will have label home, other person - address can will have label mama's place.
I can't add this property on address entity, because same address will have different labels for different persons.
Since relationships are modeled like NSSets, I don't see a way to do what I want.
Can this be somehow done?
It is not possible to add attributes to a many-many relationship directly. The Apple-recommended approach (see "Modelling a relationship based on its semantics" in the CoreData Programming Guide) is to replace the many-many relationship with an intermediate entity, to which you add the attributes. Each of your existing entities will have a one-many relationship with the new entity.
In your case, you might have something like this:
Person <--->> PersonAddressDetails <<---> Address
You can then add the label attribute to the PersonAddressDetails entity.

Predicates in coredata in objectivec

I have two tables in coredata Details and person.
Person has two fields Id and Code:
Id Code
IAS RT
IAS TP
IAS IP
Now,detailshas two fields,code and Name
Code Name
RT Rataz
TP Tranzps
IP Irrz
Now I want to use predicates so that i can first fetch the id from person,then after getting the field against the id.I can use the same id as predicate to get Name from person table.How this can be achieved.Any help or suggestion would be appreciated.Thanks in advance.
OK, how you should do this is using a relationship.
Add a relationship to Person called "details" and add a relationship to Detail called "people" (or "person" if it's singular).
Now you can get the person by the id and then use person.details to get the details associated with that person.
Core Data is not a database. Think of it more as a data model. Create the data model and object relationships that you need and use them.
You would be better off checking out a core data tutorial. It will help you in understanding how to use core data.

NSFetchedResultsController - NSSortDesciptor One-To-Many relationship

In my data model I have a Customer object and each customer can have many Address objects mapped as a one to many relationship.
I am using NSFetchedResultsController to get the data for each Customer. However I want to sort the response by city on the Address object.
Please can you show me how this can be done (if possible ta ll). I've tried a few ways but with no real luck.
Thanks

Core Data: Parent Entity Vs Relationships

I'm working on a data-collecting app and I'm having trouble gaining an understanding of how these concepts connect? Here is my scheme:
Site <----->> Station <------->> Observation Event
Site has one attribute, the name of a Site, and can containing multiple stations. Each station will have multiple observations over time. I have these set with the Event to the left as a parent event and created one-to-many relationships as diagrammed, Since each observation event will need to be tagged with site and station.
I'm assuming the parent entity is the best way to create this, or is that what a relationship would do? I expect the user would setup site/station data ahead of time and then observation data would be filled in as they were made.
In short, I just can't wrap my head around what a relationship does in core data and if a parent entity would be redundant. The core data documentation is just not clear to me on this. Any help would be vastly appreciated!!
In essence, what you're going to see when you generate your entity classes, is that in addition to the attributes of each entity you'll have an NSSet for the "to-many" relationship. You can reference any of the "records" in the to-Many relationship by the values in the set.
It seems complicated at first but then it makes total sense. So, if you want to look at the stations, you'll maybe have a "stations" set that includes a list of managed objects for each of the station entities for that site. Each station will contain a set with the managed objects for each of the related observations.
So, once you have a Site entity, you could look at all the stations for that site with something like this:
Site *site = (Site *) managedObjectForSite;
for (NSManagedObject *station in site.stations)
{
Station *stat = (Station *) station;
(do what you need to with the station record)
}
You "link" sites with stations by adding members to the stations set of a given site record, where each member is a station's managed object. You are relieved of the responsibility of "reading" station records -- once you have the members of the set which are loaded with the site, each of those is effectively a managed object for the related stations.
When the light comes on it will all be crystal clear at once. You have to work through it once then you'll pretty much know what's happening in there..
Please also see this as it may help: One-to-Many Relationship: CoreData
In Core Data, relationships have a similar function a foreign keys in a classic relational database setup.
In a database, you would "connect" the Site, Station and Event entities with a foreign key:
Site .id = Station .siteID
Station .id = Event .stationID
In core data this is not necessary. Neither of the two entities needs an extra ID attribute, instead you just define one-to-many relationships.
Site <--->> Station <---->> Event
The advantage: you can access the site from the station, or all the stations from the site with transparent and highly legible dot-notation as you would expect from an object graph. You can even conveniently get the site from an event object, etc.
Site *aStationsSite = station.site;
NSSet *aSitesStations = site.stations;
NSSet *aStationsEvents = station.events;
NSSet *sisterStations = station.site.stations;
Site *siteFromEvent = event.station.site;

Best way to save/store customer purchase order data?

I have a custom membership provider which I extended - added a couple of fields, first name, last name, adress, zip code and city.
now, these fields reside in the aspnet_Membership table so that I can easily access them when using the static Membership asp.net class.
now, I want to be able so save customer purchase order data (first name, last name, adress, zip code and city) to the database.
should I in my order model/table use a new set of fields - first name, last name, adress, zip code, city or should I create a relationship between my asp_Membershihp table and my Orders table?
Also, If i have dupe data, once a users asks for his account to be removed I wont have any orphan rows in my Orders table if I use the first method.
so, which is best, to have the user data, first name, last name, adress, zipcode, city in only one table and create a relationship between aspnet_Membership table and Orders table OR create the dupe fields in my Orders table with no relationship to the aspnet_Membership table? Pros cons?
Thanks!
/P
In this scenario, i would rather have the relationship.
Also being the data you are storing Orders (i assume at least, from the name :)) i would maintain a separate set of data on the Order, so one would optionally be able to specify different billing/shipping data than it's Identity on the site.
Another valid reason for duplicate at least some data on the Order table is to have all the necessary data relevant to an Order in the table, thus avoiding problems if the Client request his data to be deleted, and maintain the original values for that data on the order if the customer data were to change in time.
If you are able to, though, you should not actually delete User data, but have a field in which you specify if the User isActive or isDeleted.

Resources