NSPredicate不使用计算字段

时间:2014-01-21 19:23:10

标签: ios core-data nspredicate predicate calculated-field

我有一个核心数据项目,我很难用一个简单的计算字段搜索数据,并且不知道它为什么不起作用。

我有一个Tutor实体,核心数据字符串属性为“tutorFirstName”和“tutorLastName”。我创建了一个额外的字符串属性“tutorFullName”,它在类别中填充如下:

NSString *fullName = [@[self.tutorFirstName, self.tutorLastName] componentsJoinedByString:@" "];

数据填充正常,但是当我执行以下搜索时,只有tutorFirstName和tutorLastName上的谓词有效:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"tutorFirstName = %@", @"Sean"];
[fetchRequest setPredicate:predicate];
NSArray *fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error];
NSLog(@"Search on %@ returned %lu items (%@)",predicate, (unsigned long)fetchedObjects.count, [fetchedObjects objectAtIndex:0]);

predicate = [NSPredicate predicateWithFormat:@"tutorFullName = %@", @"Sean Silverman"];
[fetchRequest setPredicate:predicate];
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error];
NSLog(@"Search on %@ returned %lu items",predicate, (unsigned long)fetchedObjects.count);

predicate = [NSPredicate predicateWithFormat:@"tutorLastName = %@", @"Silverman"];
[fetchRequest setPredicate:predicate];
fetchedObjects = [moc executeFetchRequest:fetchRequest error:&error];
NSLog(@"Search on %@ returned %lu items (%@)",predicate, (unsigned long)fetchedObjects.count, [fetchedObjects objectAtIndex:0]);

以下是显示每次搜索结果的调试代码,以及生成的核心数据对象DOES具有正确的tutorFullName条目。

2014-01-21 14:05:26.129 AT Data[54935:70b] Search on tutorFirstName == "Sean" returned 1 items (<Tutor: 0x113b16830> (entity: Tutor; id: 0xd0000000041c0006 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Tutor/p263> ; data: {
charges =     (
    "0xd000000581b40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90221>",
    "0xd00000058b280002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90826>",
    "0xd00000057d4c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p89939>",
    "0xd0000005994c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91731>",
    "0xd000000593c40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91377>",
    "0xd0000005aa3c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92815>",
    "0xd0000005ab440002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92881>",
    "0xd0000005aa500002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92820>",
    "0xd00000057ea00002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90024>",
    "0xd0000005a7000002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92608>",
    "(...and 595 more...)"
);
tutorFirstName = Sean;
tutorFullName = "Sean Silverman";
tutorLastName = Silverman;

2014-01-21 14:05:26.130 AT Data[54935:70b] Search on tutorFullName == "Sean Silverman" returned 0 items

2014-01-21 14:05:26.131 AT Data[54935:70b] Search on tutorLastName == "Silverman" returned 1 items (<Tutor: 0x113b16830> (entity: Tutor; id: 0xd0000000041c0006 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Tutor/p263> ; data: {
charges =     (
    "0xd000000581b40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90221>",
    "0xd00000058b280002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90826>",
    "0xd00000057d4c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p89939>",
    "0xd0000005994c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91731>",
    "0xd000000593c40002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p91377>",
    "0xd0000005aa3c0002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92815>",
    "0xd0000005ab440002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92881>",
    "0xd0000005aa500002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92820>",
    "0xd00000057ea00002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p90024>",
    "0xd0000005a7000002 <x-coredata://B8C4C087-A68F-4CA6-935D-F596CB1E9D0C/Charge/p92608>",
    "(...and 595 more...)"
);
tutorFirstName = Sean;
tutorFullName = "Sean Silverman";
tutorLastName = Silverman;

对我来说,唯一让我感到奇怪的是,在实体的调试输出中,严格的核心数据属性没有围绕字符串值的双引号,但计算的属性确实如此(“Sean Silverman”) ,但这可能只是因为计算属性中有空格。

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:9)

通过计算,我假设你的意思是属性是暂时的。如果是这种情况,那么您将无法搜索这些属性。这是因为在您搜索的所有核心数据实体执行搜索时可能无法创建该属性。从Core Data Programming Guide(在清单1之后):

  

您无法使用基于瞬态属性的谓词进行提取(尽管您可以使用瞬态属性自行在内存中进行过滤)。

因此,如果您想使用谓词来过滤该属性,则需要使该属性不是瞬态的。