OStack程序员社区-中国程序员成长平台

标题: iphone - 核心数据一对多关系 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:17
标题: iphone - 核心数据一对多关系

我的核心数据模型中有一对多的关系。一餐可以有多种食物。我想用一顿饭的食物填满一张 table 。因此,如果第一餐有苹果、橙子和柠檬,那么餐 table 将由这三种食物组成。

非常感谢任何帮助,因为我完全陷入困境。

这是我的模型:

enter image description here

还有 NSManagedObject 类:

Meal.h

@class Food;

@interface Meal : NSManagedObject

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSSet *foods;

@end

@interface Meal (CoreDataGeneratedAccessors)

- (void)addFoodsObjectFood *)value;
- (void)removeFoodsObjectFood *)value;
- (void)addFoodsNSSet *)values;
- (void)removeFoodsNSSet *)values;

@end

食物.h

@class Meal;

@interface Food : NSManagedObject

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) Meal *meals;

@end

这是我尝试尝试的代码,但我遇到了一些问题。

- (NSFetchedResultsController *)fetchedResultsController 
{    
    if (_fetchedResultsController != nil)
        return _fetchedResultsController;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName"Food" inManagedObjectContext:_context];
    [fetchRequest setEntity:entity];

    [fetchRequest setPredicate:[NSPredicate predicateWithFormat"Meal.foods == %@", entity]];

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

    NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_context sectionNameKeyPath:nil cacheName"Root"];

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

    [fetchRequest release];
    [theFetchedResultsController release];

    return _fetchedResultsController;      
}



Best Answer-推荐答案


您提到您遇到了问题,但没有提到问题是什么;但是您的 NSFetchRequest 谓词对我来说看起来不正确。此外,Food 上定义的 meals 是复数,但关系是一对一的——有点困惑。

您的 UITableView 应该填充单个 Meal 中的 Food,对吗?我们称它为 Meal _thisMeal,那么 fetch 谓词应该是:

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

所以,您要获取的实体是 Food;这个谓词确保他们的 meals 属性是你的 UITableViewController 正在寻找的 Meal

更新以显示如何获取命名的 Meal

要获取名为“Custom Meal”的Meal:

Meal *_thisMeal = nil;
// this assumes that there should be only one Meal named "Custom Meal"
NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName"Meal"];
NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat"name = %@",@"Custom Meal"];

NSError *fetchError = nil;
NSArray *meals = [_context executeFetchRequest:request error:&fetchError];
if( fetchError )
    NSLog(@"%s - ERROR while fetching Meal: %@,%@",__FUNCTION__,fetchError,[fetchError userInfo]);
else if( [meals count] != 0 )
    _thisMeal = [meals objectAtIndex:0];

关于iphone - 核心数据一对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9695031/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4