why wont this NSPredicate work?

i have a list of objects being managed by CoreData. i want to get a specific object out of CoreData using an NSPredicate. below is the code i am using. Array arr always comes back with 0 objects in it presumably because the fetch cant find an object that matches the predicate. i know for a fact that at least 1 object in CoreData has an advertisement.uuid that matches adUdid. i have manually gotten the entire list of objects and searched it myself for the uuid and found it. advertisement is a member of WebServiceAuthService_mobileAdvertisementVO and uuid is a member of advertisement. whats even more aggregating is the fact that this exact code works just fine in another project. im at a loss to figure out why this code no longer works in the new project.

incase it matters this code is in a static library i am making.

EDIT: arr is always empty so there is nothing to post. there are also no errors being given. its just not working. the uuids are NSStrings something along the lines of "9ca98efe-ef48-47c0-aff5-058224b3093d". i have a feeling the problem may be elsewhere in the code and just manifesting itself here.

WebServiceAuthService_mobileAdvertisementVO *mobileAd = nil;

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"WebServiceAuthService_mobileAdvertisementVO" inManagedObjectContext:managedObjectContext];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"advertisement.uuid == %@",adUdid];

[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];

NSError *error = nil; 
NSArray *arr = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
[fetchRequest release];

if (error) 
{
    DLog(@"fetched special ad error: %@",[error localizedDescription]);
}

if (arr && [arr count] >= 1) 
{
    DLog(@"found ad with UUID %@",adUdid);
    for (WebServiceAuthService_mobileAdvertisementVO *obj in arr) 
    {
        NSManagedObjectID *objID = [obj objectID];
        if (![objID isTemporaryID]) 
        {
            mobileAd = obj;
        }
    }
}

You are comparing strings, in which case LIKE is a better operator than ==. So:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"advertisement.uuid LIKE %@",adUdid];

Don't worry about quotes, predicateWithFormat: will automatically put single quotes around the right hand term.

EDIT: Reference: Predicate Programming Guide

链接地址: http://www.djcxy.com/p/60630.html

上一篇: NSPredicate与Integer比较

下一篇: 为什么不用这个NSPredicate工作?