Core Data - filtering a To-Many Relationship using Predicates

前端 未结 2 1288
暖寄归人
暖寄归人 2021-02-04 09:22

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

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

I have s

相关标签:
2条回答
  • 2021-02-04 10:07

    To get Products manufactured by a manufacturer with name containing "nut", your request should look like:

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

    To get Manufacturers with product names containing "nut" your request should look like:

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

    If your result set is empty, it might be due to the fact that no objects answer the predicate (contain the substring "nut").
    Try adding some fake entities with known names and testing.

    Edit: This is the code you could use for testing:

    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]);
    }
    
    0 讨论(0)
  • 2021-02-04 10:15

    Take a look at this document from Apple. It does an example doing what you are trying to do with "ANY" predicate.

    0 讨论(0)
提交回复
热议问题