Iphone NSPredicate 如何做一个INNER JOIN? [英] Iphone NSPredicate how to do an INNER JOIN?

查看:28
本文介绍了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 个实体 Instruments 和 Deals.而交易"使用工具"来执行所需的操作.现在我想列出用于此交易的 Deals 属性和 Instrument 属性.所以在 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:

选择 *从仪器内部连接交易ON Instruments.ID = Deals.InstrumentID

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

我将如何使用 NSPredicate 完成此操作?任何帮助将不胜感激.谢谢.

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

-奥斯卡

推荐答案

首先,你应该尽你所能摆脱 SQL 思维模式.核心数据不是 ORM.它是一个对象图管理框架,恰好使用 SQLite 作为一个可能的持久性后端.尽管您可以通过环境变量(用于调试)查看它使用的 SQL,但 SQL 是私有的实现细节.Core Data 完全可以在没有任何 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.

所以,尽量不要考虑数据库表.您有一组 Deal 实例.你想要他们的属性吗?使用 键值编码获取这些实例和相关 Instrument 实例的属性.假设你有一个 DealsNSSet 实例,叫做 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"];

会给你一个 NSSetdealProperty 的值来自每个 Deal 实例.如果你想一次获取多个属性,可以使用-[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'.

要获得嵌套"属性,只需使用键路径:

To get "nested" properties, just use a key path:

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

将为您提供一组与每个 Deal 实例关联的 Instrument 实例的 instrumentProperty 值,假设 instrument 是从 DealInstrument 的一对一关系.如果关系是一对多的,您将获得一组 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆