我怎样才能用这个来做谓词?
我有一个语言实体,用于存储文本及其随附的语言代码。我需要获取用户所选语言的所有文本列表,除非该语言中没有特定的文本行,在这种情况下我需要获取英文版本。
我的表基本上是这样的,用 SQL 术语来说:
DECLARE TABLE Languages
(
textId int,
languageCode char(2),
text varchar(2000)
)
以 textId 和 languageCode 作为我的主键。
在 SQL 中,我基本上会这样做:
SELECT ISNULL(l.text, e.text)
FROM
(SELECT * FROM Languages WHERE languageCode = 'en') t
LEFT JOIN
(SELECT * FROM Languages WHERE languageCode = @selectedLanguage) l
ON l.textId = t.textId
这应该为每个 textId 提供 1 段文本;如果找不到所选语言,则使用英语。
是否可以使用 NSPredicate 和 FetchedResultsController 执行类似的操作?我发现了一些有关 SUBQUERY 关键字的信息,但没有真正解释如何将其与 NSPredicate 一起使用。
I have a languages entity that stores text and the language code it goes with. I need to get the list of all the text for the language the user has selected, unless a particular line of text isn't available in that language, in which case I need to get the English version.
My table is basically like this, in SQL terms:
DECLARE TABLE Languages
(
textId int,
languageCode char(2),
text varchar(2000)
)
With both textId and languageCode as my primary key.
In SQL, I would basically do this:
SELECT ISNULL(l.text, e.text)
FROM
(SELECT * FROM Languages WHERE languageCode = 'en') t
LEFT JOIN
(SELECT * FROM Languages WHERE languageCode = @selectedLanguage) l
ON l.textId = t.textId
That should give me exactly 1 piece of text for each textId; using English if it can't find the selectedLanguage.
Is it possible to do anything like this using NSPredicate and a FetchedResultsController? I found a little info on the SUBQUERY keyword, but no real explanation of how to use it with an NSPredicate.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以我能够以一种不同且更简单的方式执行此操作...
其中 findLanguages 是我所选择语言的所有文本的数组。它确实需要额外的获取请求来获取所选语言的所有文本列表,但它会准确返回所需的内容。比我在 SQL 中使用左连接和子查询简单得多。但效率可能较低。
Ok so I was able to do this in a different and much simpler way...
Where foundLanguages is an array of all the text that I have in the selected language. It does require an extra fetch request to get the list of all text for the selected language, but it returns exactly what's needed. MUCH simpler than using the left-join and subqueries like I was thinking in SQL. Probably less efficient though.