0

我有一个具有一对多关系的核心数据模型,有一个类别,它可以包含许多子类别。

类别 <---->> 子类别

我正在尝试执行检查特定类别是否包含具有特定名称的子类别的提取。假设我在下面有两个类别,我想查看名为“Fruits”的类别中是否有任何子类别名称为“Apple”。

 Vetegables
    - Carrot
    - Lettuce

 Fruits
    - Apple
    - Orange
    - Pear

代码:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                   inCategory:(Category *)category
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"SubCategory" inManagedObjectContext:self.beer.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@", subCategory];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if (fetchedObjects != nil && fetchedObjects.count > 0) {
        return [fetchedObjects objectAtIndex:0];
    }
    else {
        return nil;
    }
}
4

1 回答 1

2

尝试寻找具有您喜欢的子类别的类别实体,而不是相反,然后从结果中返回 category.subcategory。我不确定你的 NSManagedObjects 是什么样子,但试试这个:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                   inCategory:(Category *)category
{
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext];
    [fetchRequest setEntity:entity];

    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"subcategory.name == [c] %@ AND name == %@", subCategory, category.name];
    [fetchRequest setPredicate:predicate];

    NSError *error;
    NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

    if (fetchedObjects != nil && fetchedObjects.count > 0) {
        Category *categoryManagedObject = [fetchedObjects objectAtIndex:0];
        return categoryManagedObject.subcategory;
    }
    else {
        return nil;
    }
}

...或者只是将类别添加到您的谓词中,您应该在子类别托管对象定义中包含它:

- (SubCategory *)searchForSubCategoryWithName:(NSString *)subCategory
                                       inCategory:(Category *)category
    {
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"Category" inManagedObjectContext:self.beer.managedObjectContext];
        [fetchRequest setEntity:entity];

        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == [c] %@ AND category == %@", subCategory, category];
        [fetchRequest setPredicate:predicate];

        NSError *error;
        NSArray *fetchedObjects = [self.beer.managedObjectContext executeFetchRequest:fetchRequest error:&error];

        if (fetchedObjects != nil && fetchedObjects.count > 0) {
            return [fetchedObjects objectAtIndex:0];
        }
        else {
            return nil;
        }
    }
于 2012-10-31T22:58:59.987 回答