NSPredicate不适用于计算字段 [英] NSPredicate not working with calculated field

查看:89
本文介绍了NSPredicate不适用于计算字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Core Data项目,很难用一个简单的计算字段来搜索数据,也不知道为什么它不起作用。



我有一个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错误:&错误];
NSLog(@搜索%@返回的%lu个项目(%@),谓词,(无符号长)fetchedObjects.count,[fetchedObjects objectAtIndex:0]);

谓词= [NSPredicate predicateWithFormat:@ tutorFullName =%@,@ Sean Silverman];
[fetchRequest setPredicate:predicate];
fetchedObjects = [moc executeFetchRequest:fetchRequest错误:&错误];
NSLog(@搜索%@返回的%lu个项目,谓词,(无符号长)fetchedObjects.count);

谓词= [NSPredicate predicateWithFormat:@ tutorLastName =%@,@ Silverman];
[fetchRequest setPredicate:predicate];
fetchedObjects = [moc executeFetchRequest:fetchRequest错误:&错误];
NSLog(@搜索%@返回的%lu个项目(%@),谓词,(无符号长)fetchedObjects.count,[fetchedObjects objectAtIndex:0]);

此处是调试代码,显示了每次搜索的结果,并且结果核心数据对象确实具有正确的tutorFullName条目。

  2014-01-21 14:05:26.129 AT Data [54935:70b]在tutorFirstName =上搜索= Sean返回了1个项目(< Tutor:0x113b16830>(实体:Tutor; id:0xd0000000041c0006< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Tutor / p263>数据:{
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>,$ bbb 0xd0000005994c0002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / p91731>,
0xd000000593c40002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596B / p91377>,
0xd000 0005aa3c0002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / p92815>,
0xd0000005ab440002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1Char / 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>,
(...还有595个以上...)
);
tutorFirstName =肖恩;
tutorFullName = Sean Silverman;
tutorLastName = Silverman;

2014-01-21 14:05:26.130 AT Data [54935:70b]在tutorFullName == Sean Silverman上搜索返回0个项目

2014-01-21 14:05:26.131 AT Data [54935:70b]在tutorLastName == Silverman上搜索返回1个项目(< Tutor:0x113b16830>(实体:Tutor; id:0xd0000000041c0006< x-coredata:// B8C4C087-A68F- 4CA6-935D-F596CB1E9D0C / Tutor / p263>;数据:{
收费=(
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-4CA -935D-F596CB1E9D0C / Charge / p89939>,
0xd0000005994c0002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / p91731>,
2xd000000593 -coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / p91377>,
0xd0000005aa3c0002< x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / 815>,
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>,
0 < x-coredata:// B8C4C087-A68F-4CA6-935D-F596CB1E9D0C / Charge / p92608>,
(...和595个更多...)
);
tutorFirstName =肖恩;
tutorFullName = Sean Silverman;
tutorLastName = Silverman;

唯一让我感到奇怪的是,在实体的调试输出中,核心数据属性的字符串值周围没有双引号,但是计算出的属性却有( Sean Silverman),但这可能仅是因为计算出的属性中有空格。



任何帮助将不胜感激。

解决方案

通过计算,我假设您的意思是该属性是瞬态的。如果是这种情况,那么您将无法搜索这些属性。这是因为对于正在搜索的所有核心数据实体执行搜索时可能不会创建该属性。从核心数据编程指南(仅清单1之后):


您不能使用基于瞬态属性的谓词来获取(尽管您可以自己使用瞬态属性在内存中进行过滤) 。


因此,如果您要使用谓词对该属性进行过滤,则需要使属性为非瞬态。 / p>

I have a Core Data project and am having difficulty searching the data with a simple calculated field and have no idea why it's not working.

I have a Tutor entity, with core data string attributes "tutorFirstName" and "tutorLastName". I've created an additional string attribute "tutorFullName" which is populated in a Category as such:

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

The data is populated fine, but when I perform the following searches only the predicates on the tutorFirstName and tutorLastName work:

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]);

Here is the debug code showing the results of each search and also that the resulting core data object DOES have the correct tutorFullName entry.

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;

The only thing that seems strange to me is that, in the debug output of the entity, the strict core data attributes do not have the double-quotes around the string value, but the calculated attribute does ("Sean Silverman"), but that may only be because there's a space in the calculated attribute.

Any help would be greatly appreciated.

解决方案

By calculated, I assume you mean that the property is transient. If this is the case then you are not able to search on those properties. This is because the property may not be created at the time that the search is executed for all the core data entities you are searching on. From the Core Data Programming Guide (just after Listing 1):

You cannot fetch using a predicate based on transient properties (although you can use transient properties to filter in memory yourself).

So, if you would like to use a predicate to filter on that property you will need to make the attribute non transient.

这篇关于NSPredicate不适用于计算字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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