NSPredicate在fecthRequest和过滤数组时返回不同的结果

时间:2013-02-25 11:40:59

标签: ios cocoa-touch sqlite core-data nspredicate

我有一个Core Data SQLite类型存储,我的实体中名为Article的属性之一是名为markAsArchived的字符串属性。

我想要做的是设置获取请求以获取markAsArchived不是YES的前100个项目(字符串值,而不是bool)。

所以,这是我用来设置获取请求的代码,它返回0个结果。

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Article"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
[fetchRequest setPredicate:[NSPredicate predicateWithFormat:@"markAsArchived != 'YES'"]];
[fetchRequest setFetchLimit:100];
NSError *error = nil;
self.articles = [NSMutableArray arrayWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

if (error) {
    //Handle the error
    NSLog(@"%@", error);
}

但是,如果我采用完全相同的谓词,将其从获取请求中删除,而是使用它来过滤获取请求返回的数组,我得到一些所需的结果。这是我用来执行此操作的代码。

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Article"];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"date" ascending:NO]];
[fetchRequest setFetchLimit:100];
NSError *error = nil;
self.articles = [NSMutableArray arrayWithArray:[self.managedObjectContext executeFetchRequest:fetchRequest error:&error]];

if (error) {
    //Handle the error
    NSLog(@"%@", error);
}
self.articles = [NSMutableArray arrayWithArray:[self.articles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"markAsArchived != 'YES'"]]];

现在,我的问题是,由于我正在过滤100个获取项目的数组,因此生成的过滤数组通常最终小于100,这不是我想要的。

我的猜测是,在使用谓词设置获取请求时,我正在做一些令人难以置信的noobish。关于我可能做错什么的任何指示?

PS:将markAsArchived更改为bool属性而不是字符串属性不是一种选择。

更新:我查看了以下问题,其中没有一个似乎对我有所帮助。

NSPredicate Returns No Results with Fetch Request, Works with Array Filtering

Predicate works for array but not for fetchRequest

更新(2013年3月1日):* 据我所知,executeFetchRequest返回一个空数组,而不是nil。我仔细检查了一下。

这是我通过启动参数获得的调试输出...

2013-03-01 20:29:48.218 PROJECTNAME[753:907] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCONTENT, t0.ZDATE, t0.ZENCLOSURELENGHT, t0.ZENCLOSURETYPE, t0.ZENCLOSUREURL, t0.ZFULLTEXT, t0.ZIDENTIFIER, t0.ZIMAGE, t0.ZLINK, t0.ZMARKASARCHIVED, t0.ZREAD, t0.ZREADPOSITION, t0.ZSUMMARY, t0.ZTITLE, t0.ZUPDATED, t0.ZFEED FROM ZARTICLE t0 WHERE  t0.ZMARKASARCHIVED <> ? ORDER BY t0.ZDATE DESC LIMIT 119
2013-03-01 20:29:48.221 PROJECTNAME[753:907] CoreData: details: SQLite bind[0] = "YES"
2013-03-01 20:29:48.287 PROJECTNAME[753:907] CoreData: annotation: sql connection fetch time: 0.0689s
2013-03-01 20:29:48.289 PROJECTNAME[753:907] CoreData: annotation: fetch using NSSQLiteStatement <0x1f218d70> on entity 'Article' with sql text 'SELECT 0, t0.Z_PK, t0.Z_OPT, t0.ZCONTENT, t0.ZDATE, t0.ZENCLOSURELENGHT, t0.ZENCLOSURETYPE, t0.ZENCLOSUREURL, t0.ZFULLTEXT, t0.ZIDENTIFIER, t0.ZIMAGE, t0.ZLINK, t0.ZMARKASARCHIVED, t0.ZREAD, t0.ZREADPOSITION, t0.ZSUMMARY, t0.ZTITLE, t0.ZUPDATED, t0.ZFEED FROM ZARTICLE t0 WHERE  t0.ZMARKASARCHIVED <> ? ORDER BY t0.ZDATE DESC LIMIT 119' returned 0 rows with values: ( )
2013-03-01 20:29:48.291 PROJECTNAME[753:907] CoreData: annotation: total fetch execution time: 0.0734s for 0 rows.

2 个答案:

答案 0 :(得分:0)

假设您需要将属性markedAsArchived作为字符串(您同意这是错误的设计),则获取请求的正确谓词为:

[NSPredicate predicateWithFormat:@"markedAsArchived != %@", @"YES"]

在谓词格式字符串中将常量替换为文字是不安全的。以上是检查此属性的标准方法。

答案 1 :(得分:0)

我偶然发现了同样的问题...在我看来,它与构建谓词字符串有关。如果你使用引号,你需要小心。在我的代码中,我看到:

[NSPredicate predicateWithFormat:@"markAsArchived != 'YES'"];

但实际上可能需要:

[NSPredicate predicateWithFormat:@"markAsArchived != YES"];

我有同样的问题是NSNumber被包裹在引号中。

哈利