NSPredicate不会过滤获取请求的结果

时间:2011-08-20 02:09:32

标签: core-data nsstring 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于8/19/11在下午6:39编辑

1 个答案:

答案 0 :(得分:0)

我总是在-predicateWithFormat中使用self.uuid或%K:如:

p = [NSPredicate predicateWithFormat:@“%K ==%@”,@“uuid”,aUUID];

p = [NSPredicate predicateWithFormat:@“self.uuid ==%@”,aUUID];

享受, 安德鲁