开发者

NSPredicate is not filtering the results of a fetch request

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-0EB开发者_JAVA百科EBB741111"
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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜