SUBQUERY在NSPredicate和NSInvalidArgumentException [英] SUBQUERY in NSPredicate and NSInvalidArgumentException

查看:141
本文介绍了SUBQUERY在NSPredicate和NSInvalidArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Core Data中设置了以下模型。

  Book有一个to-many关系,称为toBookOrders,实体。反向称为toBook。 
Book有一个BOOL值属性叫isSync。

我设置了以下 NSPredicate / p>

  NSEntityDescription * entityDescription = [NSEntityDescription entityForName:@BookinManagedObjectContext:moc]; 
NSPredicate * predicate = [NSPredicate predicateWithFormat:@isSync == 0 AND SUBQUERY(toBookOrders,$ x,$ x.toBook == SELF)];

通过这个谓词,我只需要抓取尚未同步的书籍及其相关订单。 / p>

这是我收到的错误。


由于未捕获的异常而终止应用程序
'NSInvalidArgumentException',reason:'无法解析格式
stringisSync == 0 AND SUBQUERY(toBookOrders,$ x,$ x.toBook == SELF)


任何想法?

解决方案

这是您的问题的症结:

  @ isSync == 0 AND SUBQUERY(toBookOrders,$ x,$ x.toBook == SELF)

如果你把它拆分成两个subpredicates,如Scott建议,你会得到:




  • isSync == 0

  • SUBQUERY(toBookOrders,$ x,$ x.toBook = = SELF)



问题是每个 SUBQUERY 不返回true或false,因为谓词必须。它返回一个集合(一个数组),一个数组不是真或假的相同的东西。因此,在创建谓词时,您会收到一个错误,表示它是无效的格式,因为 AND 后的东西不是谓词



您可能

  @isSync == 0 AND SUBQUERY(toBookOrders,$ x,$ x.toBook == SELF)@ count> 0

这将给你一个谓词,以找到 isSync 为false的所有书,并且至少一个书 OrderBooks 是书。


I set up the following model in Core Data.

Book has a to-many relationship, called toBookOrders, with OrderBook entity. The inverse is called toBook.
Book has a BOOL value property called isSync.

I set up the following NSPredicate.

NSEntityDescription* entityDescription = [NSEntityDescription entityForName:@"Book" inManagedObjectContext:moc];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"isSync == 0 AND SUBQUERY(toBookOrders, $x, $x.toBook == SELF)"];

Through this predicate I need to grab only books that haven't been synchronized and theirs relative orders.

This is the error I receive.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "isSync == 0 AND SUBQUERY(toBookOrders, $x, $x.toBook == SELF)"

Any ideas? Thank you in advance.

解决方案

Here's the crux of your problem:

@"isSync == 0 AND SUBQUERY(toBookOrders, $x, $x.toBook == SELF)"

If you split that up into the two subpredicates, as Scott suggests, you'll get:

  • isSync == 0
  • SUBQUERY(toBookOrders, $x, $x.toBook == SELF)

The problem is that every SUBQUERY does not return true or false, as a predicate must. It returns a collection (an array), and an array is not the same thing as true or false. Thus, when you create the predicate, you're getting an error that it's an invalid format, because the stuff after the AND isn't a predicate. It's simply an expression.

You're probably wanting:

@"isSync == 0 AND SUBQUERY(toBookOrders, $x, $x.toBook == SELF).@count > 0"

This would give you a predicate to find all the books where isSync is false and the at least one of the Book's OrderBooks is that Book.

这篇关于SUBQUERY在NSPredicate和NSInvalidArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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