Kevin
Kevin

Reputation: 189

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!!

Upvotes: 0

Views: 1223

Answers (2)

Mundi
Mundi

Reputation: 80273

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;

Upvotes: 0

RegularExpression
RegularExpression

Reputation: 3541

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

Upvotes: 1

Related Questions