带有谓词的CoreData fetchRequest错误地跳过NULL值 [英] CoreData fetchRequest with predicate incorrectly skips NULL values

查看:89
本文介绍了带有谓词的CoreData fetchRequest错误地跳过NULL值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CoreData中存储的对象看起来像这样:

 
id 名称 类型
1猫aaa
2狗bbb
3猿空

我正在基于类型属性获取这些对象,如下所示:

  NSFetchRequest * fetchRequest = [ [NSFetchRequest alloc] initWithEntityName:self.entityName]; 

fetchRequest.predicate = [NSPredicate predicateWithFormat:@ type!=%@,@ aaa];
fetchRequest.fetchBatchSize = 100;
fetchRequest.sortDescriptors = @ [[[NSSortDescriptor sortDescriptorWithKey:@ type ascending:YES]];

NSFetchedResultsController * fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequestmanagedObjectContext:self.managedObjectContext sectionNameKeyPath:@ section cacheName:nil];
fetchedResultsController.delegate =自我;

NSError *错误=无;
[fetchedResultsController performFetch:& error];

因此,我只是试图获取类型不类似于 aaa的所有对象。 / p>

获取请求意外地不仅删除了谓词中指定的具有 aaa类型的对象,而且还忽略了未设置类型(空)的对象,从而导致以下内容:

 
id 名称 类型
2 bar bbb

不是我期望的:

 
id 名称 类型
2狗bbb
3猿空

当我使用谓词格式时,也会发生相同的问题: NOT(键入IN { aaa, bbb})。这是我实际需要的方式。该谓词不会导致获取任何记录。



我不知道为什么会发生这种情况,因为如果我不指定谓词,则会正确地获取所有对象。如果将谓词更改为:

  [NSPredicate predicateWithFormat:@ type = null ]; 






请注意;以上所有内容仅说明了问题的根源。我的实际代码更加复杂,仅使用谓词格式: type!= aaa OR type = null 是我想避免的东西。



任何帮助都非常感激!

解决方案

这是指定行为。参见此处:
https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794-SW1


I have objects stored in my CoreData that look something like this:

id  name  type
1   cat   aaa
2   dog   bbb
3   ape   NULL

I'm fetching these objects based on the "type" property like this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:self.entityName];

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"type != %@", @"aaa"];
fetchRequest.fetchBatchSize = 100;
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"type" ascending:YES]];

NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"section" cacheName:nil];
fetchedResultsController.delegate = self;

NSError *error = nil;
[fetchedResultsController performFetch:&error];

So, I'm simply trying to fetch all objects where type is not like "aaa".

The fetch request unexpectedly omits not only the objects with "aaa" types as specified in the predicate, but also the objects where the type is not set (null), resulting in the following:

id  name  type
2   bar   bbb

Instead of what I'd expect:

id  name  type
2   dog   bbb
3   ape   NULL

The same problem happens when I use predicate format: NOT (type IN {"aaa", "bbb"}). Which is the actual way I'll be needing this. This predicate results in no records getting fetched.

I fail to see why this could happen, because if I don't specify a predicate all objects get fetched properly. Objects with NULL types do get fetched correctly if I'd change the predicate to:

[NSPredicate predicateWithFormat:@"type = null"];


Please note; all of the above just illustrates what the problem boils down to. My actual code is more complicated and simply using the predicate format: type != "aaa" OR type = null is something I'd like to avoid if possible.

Any help greatly appreciated!

解决方案

It's designated behavior. See here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Predicates/Articles/pUsing.html#//apple_ref/doc/uid/TP40001794-SW1

这篇关于带有谓词的CoreData fetchRequest错误地跳过NULL值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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