谓词适用于数组但不适用于 fetchRequest [英] Predicate works for array but not for fetchRequest

查看:85
本文介绍了谓词适用于数组但不适用于 fetchRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 fetchRequest 来显示其他数组中尚未存在的所有值.此代码返回我期望的数组:

I am trying to create a fetchRequest that shows me all values that are not already in another array. This code returns the array I expect:

NSArray *questionChoices = [self.currentQuestionChoiceGroup.questionChoices allObjects]; 
NSArray *filteredQuestionChoices  = [questionChoices filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"NONE %@ == code", myarr]];

myarr 包含 1 个项目,这 1 个项目从过滤结果中排除,正如预期的那样.但是,此代码不起作用:

myarr contains 1 item, and that 1 item is excluded from the filtered results, as expected. However, this code doesn't work:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext];
request.predicate = [NSPredicate predicateWithFormat:@"NONE %@ == code AND questionChoiceGroup == %@", myarr, self.currentQuestionChoiceGroup];

当我执行这个 fetch 请求时,我得到了属于当前 questionChoiceGroup 的所有 questionChoices,包括在 myarr 中有代码的那个.似乎完全忽略了AND语句的第一部分.

When I execute this fetch request, I get all the questionChoices that belong to the current questionChoiceGroup, including the one that has a code that's in myarr. It seems to completely ignore the first part of the AND statement.

我看不出两者之间有什么区别会导致不同的结果.有人可以帮忙吗?

I can't see any difference between the two that should lead to different results. Can anyone help?

EDIT 更简单的解释:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"questionChoiceGroup == %@ AND NONE %@ == code", self.currentQuestionChoiceGroup, [NSArray arrayWithObject:@"A"]];

NSFetchRequest *request = [[NSFetchRequest alloc] init];
request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"displayOrder" ascending:YES]];
request.entity = [NSEntityDescription entityForName:@"QuestionChoice" inManagedObjectContext:self.managedObjectContext];

request.predicate = predicate;
NSArray *filteredQuestionChoices1 = [self.managedObjectContext executeFetchRequest:request error:nil];
NSLog(@"my filtered question choices 1: %@", [filteredQuestionChoices1 valueForKey:@"code"]);   

NSArray *filteredQuestionChoices2  = [filteredQuestionChoices1 filteredArrayUsingPredicate:predicate];
NSLog(@"my filtered question choices 2: %@", [filteredQuestionChoices2 valueForKey:@"code"]);

filteredQuestionChoices1 包含一个代码为A"的项目.filteredQuestionChoices2 没有该项目.第二个谓词如何过滤掉第一个谓词没有过滤掉的任何东西?两个语句都使用完全相同的谓词.如果在数组上使用谓词时,完全相同的谓词会过滤掉这些对象,为什么我要从 fetchedRequest 中取回对象?

filteredQuestionChoices1 contains an item with a code of "A". filteredQuestionChoices2 does not have that item. How can the second predicate filter out anything that the first predicate didn't filter out? Both statements use the exact same predicate. Why am I getting objects back from the fetchedRequest if the exact same predicate will filter those objects out if I use the predicate on the array?

推荐答案

查看 核心数据指南中的获取谓词和排序描述符.

如果您使用基于 SQL 的数据存储(例如 SQLite),那么谓词将转换为 SQL 并由数据库调用,而不是由 Cocoa 调用.因此,您可以获得与标准数组不同的行为,并且对支持的内容也有限制.具体来说,ALLANYIN 操作是有限制的(你的 NONE 操作符相当于NOT( ANY ... )).

If you are using an SQL based data store (eg, SQLite) then the predicate is translated to SQL and invoked by the database, not by Cocoa. So you can get different behavior than you would see against a standard array and there are also constraints on what is supported. Specifically, there are limits on ALL, ANY, and IN operations (and your NONE operator is the equivalent of NOT( ANY ... )).

看起来您正在尝试对作为参数提供的数组执行 NONE 操作,而不是从数据库中提取的数据数组.我敢打赌,这就是 SQL 翻译谓词的绊脚石.

It looks you're trying to do your NONE operation against an array that you're supplying as an argument, rather than an array of data pulled from within the database. I'm willing to bet that's where the predicate to SQL translation is stumbling.

这篇关于谓词适用于数组但不适用于 fetchRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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