2 garaster garaster 于 2012.11.01 10:09 提问

关于核心数据判定的问题

我用到一个核心数据模型,一对多关系模式。其中一个category,包含很多subcategories。

Category <---->> Subcategory

我想做到,判断指定的Category中是否包括具体名称的Subcategory

比如,现在已经有两个category,需要得知命名为Fruit的category中是否包括名为Apple的subcategory。

 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;
    }
}

1个回答

g989_1314125
g989_1314125   2012.11.01 14:18
已采纳

我不清楚你的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;
    }
}

也可以添加category到predicate中,需要在subcategory管理对象定义:

- (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;
        }
    }
garaster
garaster 谢谢!真好用~
5 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!