15

I have the following two entities in my Core Data Model:

Manufacture {name, ...other attributes}
Product {name, .... other attributes}

I have setup a One to Many Relationship:

Manufacturer.manufactures <------>> Product.manufacturedBy

I am trying to build a predicate to return all Products belonging to Manufacturers that match a search string. E.g. if there are two Manufacturers, "King Nut", and "Queen Nut" then a search on "Nut" should return all products made by both King Nut and Queen Nut.

My predicate works perfectly when my filter is on the Product entity, however I cannot get any predicate to work when filtering on the Manufacturer entity. The result set is empty.

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:[GBKDB context]];
searchValue = @"nut";
NSString *wildcardString = [NSString stringWithFormat:@"*%@*", searchValue];

I have tried the following:

    predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchValue];
    predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name like %@",wildcardString];
    predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name matches %@",wildcardString];
    predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like %@",wildcardString];
    predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like[cd] %@",@wildcardString];
4

2 回答 2

20

要获得Product名称包含“nut”的制造商制造的 s,您的请求应如下所示:

NSString* searchVal = @"nut";
NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"];
[r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]];

要获取Manufacturer包含“nut”的产品名称的 s,您的请求应如下所示:

NSString* searchVal = @"nut";
NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Manufacture"];
[r setPredicate:[NSPredicate predicateWithFormat:@"ANY manufactures.name CONTAINS[cd] %@",searchVal]];

如果您的结果集为空,可能是因为没有对象回答谓词(包含子字符串“nut”)。
尝试添加一些具有已知名称的虚假实体并进行测试。

编辑: 这是您可以用于测试的代码:

typedef void(^config_block_t)(id);

- (void) synthesizeObjectsOfEntity:(NSString*)entity
                           context:(NSManagedObjectContext*)context
                             count:(NSUInteger)count
                       configBlock:(config_block_t)configBlock
{
    for (;count;--count) {
        NSManagedObject* object = [NSEntityDescription insertNewObjectForEntityForName:entity
                                                                inManagedObjectContext:context];
        configBlock(object);
    }
}

- (void) synthesizeProductsAndManufacturersInContext:(NSManagedObjectContext*)context
{
    NSMutableArray* manufacturers = [NSMutableArray new];
    [self synthesizeObjectsOfEntity:@"Manufactur"
                            context:context
                              count:10
                        configBlock:^(Manufactur* m) {
                            m.name = [NSString stringWithFormat:@"m-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10];
                            [manufacturers addObject:m];
                        }];
    [self synthesizeObjectsOfEntity:@"Product"
                            context:context
                              count:100
                        configBlock:^(Product* p) {
                            p.name = [NSString stringWithFormat:@"p-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10];
                            p.manufacturedBy = manufacturers[arc4random() % [manufacturers count]];
                        }];
    [context save:NULL];
    [context reset];
    NSString* searchVal = @"3";
    NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"];
    [r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]];
    NSArray* match = [context executeFetchRequest:r error:NULL];
    NSLog(@"matched: %u",[match count]);
}
于 2013-05-27T17:59:55.470 回答
5

看看Apple 的这份文件。它做了一个例子来做你试图用“ANY”谓词做的事情。

于 2013-05-27T03:55:10.547 回答