我有一个 Core Data SQLite 类型存储,我的实体中名为 Article 的属性之一是一个名为 markAsArchived 的字符串属性。
我想做的是设置一个获取请求来获取前 100 个 markAsArchived 不是 YES(字符串值,不是 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);
}
但是,如果我采用完全相同的谓词,将其从 fetch 请求中删除,而是使用它来过滤 fetch 请求返回的数组,我会得到一些想要的结果。这是我用来执行此操作的代码。
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,这不是我想要的。
我的猜测是,在使用谓词设置获取请求时,我正在做一些令人难以置信的事情。关于我可能做错了什么的任何指示?
PS:将 markAsArchived 更改为 bool 属性而不是 string 属性不是一种选择。
更新:我查看了以下问题,但似乎没有一个对我有帮助。
NSPredicate 使用 Fetch 请求不返回任何结果,适用于数组过滤
更新(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.