开发者社区> 问答> 正文

关于核心数据判定的问题

我用到一个核心数据模型,一对多关系模式。其中一个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;
    }
}

展开
收起
爵霸 2016-05-27 11:38:09 1977 0
1 条回答
写回答
取消 提交回答
  • 我不清楚你的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;
            }
        }
    2019-07-17 19:17:34
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
“静态调用链路发现”应用场景分析及实践探索 立即下载
动态、高效,蚂蚁动态卡片的内核逻辑 立即下载
典型业务逻辑漏洞挖掘 立即下载