NSPredicate 不过滤获取请求的结果
好的。这个让我打败了。
CoreData 图与您在下面看到的代码相匹配。
我想通过继承的 UUID 过滤实体。我使用以下代码创建谓词和获取请求:
NSString *aUUID = [anObjectDescription objectForKey:@"UUID"];
NSEntityDescription *entity = [NSEntityDescription entityForName:className
inManagedObjectContext:theContext];
//see if the entity already exists
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSLog(@"blah the uuid is %@",aUUID);
NSLog(@"Length: %d String:[%@]", [aUUID length], aUUID);
NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"uuid LIKE %@",aUUID];
//NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"nonExisting==7"];
NSLog(@"predicate is: %@",fetchPredicate);
[request setPredicate:fetchPredicate];
NSError *aFetchError = nil;
NSMutableArray *foundEntities = [[theContext executeFetchRequest:request error:&aFetchError] mutableCopy];
//if the entity already exists then ignore the create message.
NSLog(@"found %u entities error: %@",[foundEntities count],aFetchError);
if ([foundEntities count] == 0) {
//create
NSLog(@"!!!!!!!!!!!!!!!!!!!!!!creating!!!!!!!!!!!!!!!!");
NSManagedObject *anObject = [NSEntityDescription insertNewObjectForEntityForName:className
inManagedObjectContext:theContext];
Trackable *asTrackable = (Trackable*)anObject;
asTrackable.isRemoteData = [NSNumber numberWithBool: YES];
returnEntity = anObject;
}
else{
NSLog(@"!!!!!!!!!!!!!!!!!!!!!!found!!!!!!!!!!!!!!!!");
returnEntity = [foundEntities objectAtIndex:0];
}
无论使用什么字符串创建谓词,我都会获取表中的所有值。是的,我尝试过“UUID LIKE %@”、“UUID MATCHES %@”、“UUID == %@”以及大量的变体。它们都以相同的方式工作。不进行任何过滤。
这是上面看到的代码的输出。除了谓词字符串的日志之外,我尝试过的其他更改对您在此处看到的结果没有任何影响。应该没有匹配结果。我已经三次检查,确保此处请求的实体不在数据库中。除了一个具有不同 UUID 的实体之外,它是干净的。
2011-08-19 20:05:56.446 QC DBSync Example[28059:b603] working with a GameResult
2011-08-19 20:05:56.447 QC DBSync Example[28059:b603] blah the uuid is 1390FCDF-AFD1-4828-8CCC-0EBEBB741111
2011-08-19 20:05:56.447 QC DBSync Example[28059:b603] Length: 36 String:[1390FCDF-AFD1-4828-8CCC-0EBEBB741111]
2011-08-19 20:05:56.448 QC DBSync Example[28059:b603] predicate is: uuid LIKE "1390FCDF-AFD1-4828-8CCC-0EBEBB741111"
2011-08-19 20:05:56.449 QC DBSync Example[28059:b603] found 1 entities error: (null)
2011-08-19 20:05:56.449 QC DBSync Example[28059:b603] !!!!!!!!!!!!!!!!!!!!!!found!!!!!!!!!!!!!!!!
任何想法将不胜感激。我已经被困了几个小时了。
消息由 YENRAB 于 2011 年 8 月 19 日下午 6:39 编辑
OK. This one has me beat.
The CoreData graph matches the code you see below.
I want to filter Entities by the inherited UUID. I am creating a predicate and a fetch request using the following code:
NSString *aUUID = [anObjectDescription objectForKey:@"UUID"];
NSEntityDescription *entity = [NSEntityDescription entityForName:className
inManagedObjectContext:theContext];
//see if the entity already exists
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
NSLog(@"blah the uuid is %@",aUUID);
NSLog(@"Length: %d String:[%@]", [aUUID length], aUUID);
NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"uuid LIKE %@",aUUID];
//NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"nonExisting==7"];
NSLog(@"predicate is: %@",fetchPredicate);
[request setPredicate:fetchPredicate];
NSError *aFetchError = nil;
NSMutableArray *foundEntities = [[theContext executeFetchRequest:request error:&aFetchError] mutableCopy];
//if the entity already exists then ignore the create message.
NSLog(@"found %u entities error: %@",[foundEntities count],aFetchError);
if ([foundEntities count] == 0) {
//create
NSLog(@"!!!!!!!!!!!!!!!!!!!!!!creating!!!!!!!!!!!!!!!!");
NSManagedObject *anObject = [NSEntityDescription insertNewObjectForEntityForName:className
inManagedObjectContext:theContext];
Trackable *asTrackable = (Trackable*)anObject;
asTrackable.isRemoteData = [NSNumber numberWithBool: YES];
returnEntity = anObject;
}
else{
NSLog(@"!!!!!!!!!!!!!!!!!!!!!!found!!!!!!!!!!!!!!!!");
returnEntity = [foundEntities objectAtIndex:0];
}
Regardless of what string the predicate is created with I get all of the values in the table. Yes I have tried "UUID LIKE %@", "UUID MATCHES %@", "UUID == %@", and a huge number of variations. All of them work the same way. No filtering is done.
Here is the output of the code as you see it above. The other changes I have tried make no difference to the results you see here other than the log of what the predicate string is. There should have been no matching results. I have triple checked that the entity being requested here is not in the database. It is clean except for one entity that has a different UUID.
2011-08-19 20:05:56.446 QC DBSync Example[28059:b603] working with a GameResult
2011-08-19 20:05:56.447 QC DBSync Example[28059:b603] blah the uuid is 1390FCDF-AFD1-4828-8CCC-0EBEBB741111
2011-08-19 20:05:56.447 QC DBSync Example[28059:b603] Length: 36 String:[1390FCDF-AFD1-4828-8CCC-0EBEBB741111]
2011-08-19 20:05:56.448 QC DBSync Example[28059:b603] predicate is: uuid LIKE "1390FCDF-AFD1-4828-8CCC-0EBEBB741111"
2011-08-19 20:05:56.449 QC DBSync Example[28059:b603] found 1 entities error: (null)
2011-08-19 20:05:56.449 QC DBSync Example[28059:b603] !!!!!!!!!!!!!!!!!!!!!!found!!!!!!!!!!!!!!!!
Any ideas would be appreciated. I've been stuck for hours.
Message was edited by yenrab on 8/19/11 at 6:39 PM
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Lee,
我总是在 -predicateWithFormat: 中使用 self.uuid 或 %K,如下所示:
p = [NSPredicate predicateWithFormat:@"%K == %@", @"uuid", aUUID];
或者
p = [NSPredicate predicateWithFormat:@"self.uuid == %@", aUUID];
享受,
安德鲁
Lee,
I always use either self.uuid or %K in -predicateWithFormat: as in:
p = [NSPredicate predicateWithFormat:@"%K == %@", @"uuid", aUUID];
or
p = [NSPredicate predicateWithFormat:@"self.uuid == %@", aUUID];
Enjoy,
Andrew