什么是构建复杂的NSCompoundPredicate的最好方法? [英] What is the best way to build a complex NSCompoundPredicate?

查看:139
本文介绍了什么是构建复杂的NSCompoundPredicate的最好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用许多数据构建一个 NSPredicate 。例如在SQL中我会做如下:

I need to build an NSPredicate with many pieces of data. For example in SQL I would do something like the following:

SELECT * 
  FROM TRANSACTIONS
  WHERE CATEGORY IN (categoryList)
    AND LOCATION IN (locationList)
    AND TYPE IN (typeList)
    AND NOTE contains[cd] "some text"
    AND DATE >= fromDate
    AND DATE <+ toDate

我正在努力如何构建为 NSPredicate 以与Core Data一起使用。我已经阅读文档...这只提供了简单的例子。如果任何人可以指出一个更复杂的例子,我一定会很感激。

I'm struggling with how to build this as an NSPredicate for use with Core Data. I've read the documentation... which only provides simplistic examples. If anybody can point me to a more complex example I would certainly appreciate it.

两年了很多人发现有帮助。我的讯息已删除。以下是解决方案的更新网址。

Well, I had an answer out here for two years that many people found helpful. My post was deleted. Here is the updated URL with the solution.

https://www.radeeccles.com/convert-sql-statement-to-an-nspredicate-for-use-with-core-data/

推荐答案

您需要做的是为每个子句创建一个谓语。例如,我们分解您的查询:

What you need to do is to create a Predicate for each one of your clauses. For example let's break down your query:


  1. SELECT * FROM TRANSACTIONS

  2. WHERE CATEGORY IN )

  3. AND LOCATION IN(locationList)

  4. AND TYPE IN(typeList)

  5. cd]some text

  6. AND DATE> = fromDate AND DATE< + toDate

  1. SELECT * FROM TRANSACTIONS
  2. WHERE CATEGORY IN (categoryList)
  3. AND LOCATION IN (locationList)
  4. AND TYPE IN (typeList)
  5. AND NOTE contains[cd] "some text"
  6. AND DATE >= fromDate AND DATE <+ toDate

基于此,你有5个谓词(2-6)。所以,让我们一个一个地工作。

Based on this, you have 5 predicates (2-6). So let's work on them one by one.

 NSPredicate *inCategoryPredicate = [NSPredicate predicateWithFormat:@"Category IN %@", categoryList];

 NSPredicate *locationPredicate = [NSPredicate predicateWithFormat:@"Location IN %@", locationList];

 NSPredicate *typePredicate = [NSPredicate predicateWithFormat:@"Type IN %@", typeList];

 NSPredicate *notePredicate = [NSPredicate predicateWithFormat:@"Note contains[c] %@", @"Some Text"];

 NSPredicate *startDatePredicate = [NSPredicate predicateWithFormat:@"Date => @", fromDate];

 NSPredicate *endDatePredicate = [NSPredicate predicateWithFormat:@"Date <= @", toDate];

现在,您只需要将它们连接到一个谓词:苹果的文档状态

Now you just need to join them into just one predicate: Apple's documentation states:


您应该构造复合谓词以最小化
工作的数量。正则表达式匹配特别是昂贵的
操作。因此,在复合谓词中,您应该在正则表达式之前执行
简单测试;

You should structure compound predicates to minimize the amount of work done. Regular expression matching in particular is an expensive operation. In a compound predicate, you should therefore perform simple tests before a regular expression;

与容易谓词第一。所以:

This being said then you should start with the "easy" predicates first. So:

NSCompoundPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects: startDatePredicate, endDatePredicate, inCategoryPredicate, locationPredicate, typePredicate, notePredicate];

如果你NSlog的话,你总是可以得到一个谓词(sql where)的样子。

You can always get an idea of what the predicate (sql where) looks like if you NSLog it.

这篇关于什么是构建复杂的NSCompoundPredicate的最好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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