iOS复合谓词 [英] iOS Compound Predicates

查看:127
本文介绍了iOS复合谓词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个有照片数据库的应用程序。每张照片都有几个标签与它相关联,应用程序有一个搜索页面,有很多的切换,允许用户搜索照片,只基于他们感兴趣的标签。每个标记都存储了整数 ID,因为它们对应于外部数据库的ID,因此我试图仅通过ID查找它们。 所有ID字段都已建立索引。

I am writing an application that has a database of photos. Each photo has several tags associated with it and the application has a search page with a lot of toggles that allows users to search for photos based on only the tags that are of interest to them. Each of these tags have stored integer ID's because they correspond to an external database's IDs, so I am trying to look them up simply by ID. All of the ID fields are indexed.

当我写的谓词变得相当大时会出现问题,因为用户可以从批次中选择的不同标签过滤。这些标签分为3类[出版物,品牌&产品],因此我的查询设置为在某个类别中OR,并在这些类别中AND。

The problem arrises when the predicates that I am writing become quite large because the user can select from lots of different tags to filter on. Those tags are grouped into 3 categories [publications, brands & products], and so my queries are setup to be 'OR'ed within a category, and 'AND'ed across these categories.

示例查询谓词类似这样:

An example query predicate ends up looking something like this:

(parentPublication.publicationId==1 OR parentPublication.publicationId==2 OR parentPublication.publicationId==5) 
AND (ANY brands.brandId==12 OR ANY brands.brandId==2 OR ANY brands.brandId==0 OR ANY brands.brandId==3 OR ANY brands.brandId==5 OR ANY brands.brandId==6 OR ANY brands.brandId==7) 
AND (ANY products.productId==2 OR ANY products.productId==3 OR ANY products.productId==6)

他们可以得到更大,但你得到的点。我的问题是,由于所有这些都是执行昂贵的连接,一旦用户选择超过10或15,查询运行非常长,甚至可能导致应用程序崩溃。

They can get much larger, but you get the point. My issue is, since all of these are performing expensive joins, once the user selects more than 10 or 15, the queries run extremely long, and can even cause the app to crash.

它似乎更有效率,写如下:

It seems like it would be more efficient to write something like:

parentPublication.publicationId IN (1,2,5) 
AND (ANY brands.brandId IN (12,2,0,3,5,6,7))
AND (ANY products.productId IN (2,3,6))

但是我似乎无法使用该语法。

But I cannot seem to get that syntax to work.

所以我的问题是:这种类型的语法支持,如果是这样,有人可以告诉我如何正确地写它吗?

So my question is: is this type of syntax supported, and if so, can someone show me how to properly write it?

还是有更好的方法来处理核心数据的这种类型的查询?

Or is there a better way to approach this type of queries against Core Data all together?

推荐答案

使用收集谓词,例如

[NSPredicate predicateWithFormat:@"parentPublication.publicationID in %@", pubIDs];
[NSPredicate predicateWithFormat:@"brands.brandID in %@", brandIDs];
[NSPredicate predicateWithFormat:@"product.productID in %@", productIDs];

现在你只需要保留三个属性数组,并创建一个三元复合谓词 andPredicateWithSubpredicates

Now you simply have to keep three arrays of attributes and make a three-way compound predicate with andPredicateWithSubpredicates.

[NSCompoundPredicate andPredicateWithSubPredicates:@[
   pubPredicate, brandPredicate, productPredicate]];

执行完毕。

这篇关于iOS复合谓词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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