核心数据一对多获取数据 [英] Core Data One-To-Many Fetch Data

查看:111
本文介绍了核心数据一对多获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在核心数据中有一对多的关系。膳食可以由许多不同的食物组成。我的代码似乎不工作在Fetch控制器。我可以自信地说,自我是我现在要吃的膳食。我通过应用程序共享上下文。

Okay I have a one-to-many relationship in core data. A Meal can consists of many different Food. My code doesn't quite seem to be working in the Fetch Controller. I can say this with confidence, the self.meal is the current Meal I am trying to fetch. I am sharing the context through out the application.

我遇到的问题是,应用程序正在显示食物,但它似乎不符合什么应该在一顿膳食内。一旦我添加一个食物,它立即显示,即使它不是在膳食内。

The problem I am having is that the application is displaying foods but it doesn't quite seem to match up against what should be inside of a Meal. As soon as I add a food, it immediately displays that, even if it is not inside of the Meal.

任何帮助或建议,我正在这样正确提取。

Any help or suggestion, I am doing this fetch correctly.

- (NSFetchedResultsController *)fetchedResultsController 
{       
    self.context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 

    if (_fetchedResultsController != nil) {
        return _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Food" inManagedObjectContext:self.context];

    [fetchRequest setEntity:entity];


    NSPredicate *foodPredicate = [NSPredicate predicateWithFormat:@"meals == %@", self.meal];
    [fetchRequest setPredicate:foodPredicate];

    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO];
    [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
    [fetchRequest setFetchBatchSize:20];

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:@"Root"];


    self.fetchedResultsController = theFetchedResultsController;
    _fetchedResultsController.delegate = self;

    [fetchRequest release];
    [theFetchedResultsController release];

    return _fetchedResultsController;
}


推荐答案

> self.meals 会 nil 那么 NSFetchedResultsController 不属于膳食一部分的食物,因此说明您注意到的行为。

If self.meals were nil then the NSFetchedResultsController would return Foods that are not part of a Meal, thereby accounting for the behavior you're noticing.

这篇关于核心数据一对多获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆