在下面的示例中,为什么两个 properties
数组没有相同的元素?第一个数组为空,第二个数组包含 1 个元素,但它们使用相同的谓词...
NSError *error;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"publication.store.storageMode != 0 && publication.store.complete == NO && publication.download == NIL"];
NSFetchRequest *request1 = [NSFetchRequest fetchRequestWithEntityName:@"Property"];
request1.predicate = predicate;
NSArray *properties1 = [self executeFetchRequest:request1 error:&error];
NSFetchRequest *request2 = [NSFetchRequest fetchRequestWithEntityName:@"Property"];
NSArray *allProperties = [self executeFetchRequest:request2 error:&error];
NSArray *properties2 = [allProperties filteredArrayUsingPredicate:predicate];
properties1
有 0 个元素。allProperties
有 12 个元素。properties2
有 1 个元素。
我不明白为什么第一个请求不返回我在第二个方法中获得的一个元素。它们不应该是等价的吗?
最佳答案
这可能不是一个答案,我不知道如何在评论中编写如此长的文本。我不太确定您不能直接在 CoreData 谓词中使用 BOOL 进行获取。
因此,出于测试目的,我会尝试使用两个不同的谓词:
NSPredicate *predicateArray = [NSPredicate predicateWithFormat:@"publication.store.storageMode != 0 && publication.store.complete == NO && publication.download == NIL"];
NSPredicate *predicateCoreData = [NSPredicate predicateWithFormat:@"publication.store.storageMode != 0 && publication.store.complete == %@ && publication.download == NIL",[NSNumber numberWithBool:myBool]];
此外,我会将 NIL 替换为 nil。
关于cocoa - 核心数据获取中的谓词不会返回与数组上相同谓词相同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18957488/