First, you should do what you can to get out of the SQL mindset. Core Data is not an ORM. It is an object graph management framework that just happens to use SQLite as one possible persistence backend. Although you can view the SQL it uses via environment variables (for debugging), the SQL is a private implementation detail. Core Data could have been implemented without any SQL at all.
So, try not to think in terms of db tables. You have a collection of Deal
isntances. You want their properties? Use Key-value Coding to get the properties of those instances and the related Instrument
instances. Assuming you have an NSSet
instance of Deals
, called deals
:
[deals valueForKey:@"dealProperty"];
will give you an NSSet
of the values of dealProperty
from each Deal
instance. If you want to get multiple properties at once, you can use -[NSObject(NSKeyValueCoding) dictionaryWithValuesForKeys:]
. You can only get keys "one level deep" using this method, so only, e.g. 'dealProperty1', 'dealProperty2' but not 'dealRelation.relationProperty' or 'dealRelation.@count'.
To get "nested" properties, just use a key path:
[deals valueForKeyPath:@"instrument.instrumentProperty"];
will give you a set of the values of the instrumentProperty
of the Instrument
instance associated with each Deal
instance, assuming instrument
is a one-to-one relationship from Deal
to Instrument
. If the relationship is one-to-many, you will get a set of sets of instrumentProperty
values.
You can always do this more explicitly (obviously this code doesn't do any thing and isn't even syntactically correct since I've omitted semicolons etc., but it shows the outline):
for(Deal *deal in deals) {
//use attribute access to get properties of deal and associated instrument
deal.dealProperty
deal.instrument.instrumentProperty //for one-to-one
for(Instrument *instrument in deal.instruments) { //for one-to-many instruments
instrument.instrumentProperty;
}
}
using enumeration of the appropriate collections.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…