本文介绍了Iphone NSPredicate如何做一个INNER JOIN?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找文档和一些其他的帖子在这个网站,但我根本不明白如何工作,这实际上是很简单的SQL。

I have been looking thru the documentation and some other posts in this site, but I simply cannot understand how to work this out, which is actually very simple in SQL.

基本上我有2个实体仪器和交易。而交易使用仪器执行所需的操作。现在,我想列出交易属性和用于此交易的文书属性。所以在SQL我会做:

Basically I have 2 entities Instruments and Deals. And the "Deals" use an "Instrument" to perform a desired operation. Now I want to list Deals attributes and the Instrument attributes that were used for this deal. So in SQL i would do:

SELECT *
FROM instruments
INNER JOIN交易
ON Instruments.ID = Deals.InstrumentID

SELECT *FROM InstrumentsINNER JOIN DealsON Instruments.ID = Deals.InstrumentID

我如何使用NSPredicate实现?任何帮助将大大apreciated。谢谢。

How would I accomplish this with an NSPredicate?. Any help will be greatly apreciated. Thank you.

-Oscar

推荐答案

你可以摆脱SQL的心态。核心数据不是一个ORM。它是一个对象图管理框架,只是发生使用SQLite作为一个可能的持久性后端。虽然您可以通过环境变量查看其使用的SQL(用于调试),但SQL是一个私有实现详细信息。核心数据可以在没有任何SQL的情况下实现。

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.

所以,尽量不要考虑数据库表。您有交易 isntances的集合。你想要他们的属性?使用获取这些实例的属性以及相关的 Instrument 实例。假设您有 NSSet 实例交易,称为 deals

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"];

会为您提供 NSSet 每个交易实例的 dealProperty 如果你想一次获得多个属性,可以使用 - [NSObject(NSKeyValueCoding)dictionaryWithValuesForKeys:] 。你只能使用这种方法获得一级深的密钥,所以只有,例如。 'dealProperty1','dealProperty2',而不是'dealRelation.relationProperty'或'dealRelation。@ count'。

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'.

要获得嵌套属性,

[deals valueForKeyPath:@"instrument.instrumentProperty"];

将为您提供 instrumentProperty 与每个交易实例相关联的 Instrument 实例,假设 c $ c>是交易仪器之间的一对一关系。如果关系是一对多的,您将获得一组 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;
  }
}

使用相应集合的枚举。

这篇关于Iphone NSPredicate如何做一个INNER JOIN?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 19:46