我刚刚开始学习如何在iOS上实现核心数据模型.在一些关于如何存储和检索实体之间一对一关系的数据的基础教程之后,我现在正在尝试实现一对多的关系.我的数据模型由两个实体组成,其各自的类定义如下:
Restaurant: @interface Restaurant :NSObject @property (nonatomic,strong) NSString *name; @property (nonatomic,strong) NSMutableArray *user_reviews; /* One to Many Relation to Review Entity*/ @end Review: @interface Review : NSObject @property (nonatomic,strong) NSString *rating; @property (nonatomic,strong) NSString *review_text; @property (nonatomic,strong) User *user; @end
我发现使用NSMutableSet插入的类似问题,但是我无法为NSMutable Array实现相同的问题.
目前我的插入代码看起来像:
NSManagedObjectContext *context = [self managedObjectContext]; Restaurant *rest = [NSEntityDescription insertNewObjectForEntityForName:@"Restaurant" inManagedObjectContext:context]; rest.name = restaurant.name;
我通过JSON获取餐厅及其评论的数据,并将其存储在临时类中,然后将其保存到CORE数据库.
如何插入有一对多关系的数据?
编辑:
目前我收到一个类的形式的数据,它定义了user_review属性
NSMutableArray *user_reviews;
我正在尝试实现这个相同的类插入核心数据模型.
但核心数据模型使用NSSet而不是NSMutableArray.
一个粗暴的方法是复制所有具有相同属性的类,而不是使用NSMutableArray,我将其更改为NSSet.
但是这会产生大量的冗余代码.不应该有更有效的方式来做到这一点吗?
解决方法
如果您的评论是您的coreDataModel中的关系,请使用NSSet可以替代NSSet,并将其与Restaurant Entity连接.
评论:
在餐厅:
如果你让xcode生成你的课程,它将如下所示:
Restaurant: @interface Restaurant : NSManagedObject @property (nonatomic,strong) NSSet *user_reviews; /* One to Many Relation to Review Entity*/ @interface Restaurant(CoreDataGeneratedAccessors) - (void)addUser_reviewsObject:(Review *)value; - (void)removeUser_reviewsObject:(Review *)value; - (void)addUser_reviews:(NSSet *)value; - (void)removeUser_reviews:(NSSet *)value; @end Review: @interface Review : NSManagedObject @property (nonatomic,strong) User *user; @property (nonatomic,strong) Restaurant *restaurant; @end
你的电话将是:
NSManagedObjectContext *context = [self managedObjectContext]; Restaurant *rest = [NSEntityDescription insertNewObjectForEntityForName:@"Restaurant" inManagedObjectContext:context]; rest.name = restaurant.name; Review *rev = [NSEntityDescription insertNewObjectForEntityForName:@"Review" inManagedObjectContext:context]; rev.rating = @"1"; rev.review_text = @"nomnomnom"; [rest addUser_reviewsObject:rev]; // or rev.restaurant = restaurant; one of both is enought as far as I remember // save your context
编辑
如果它必须是一个NSMutableArray,它不能是一个ralation.
那些总是NSSets(如果x到n)或目的地类.使用NSMutableArray具有集合和自动处理的优点.
但是如果你真的要存储在NSMutableArray中,我建议至少通过一个reviewID属性(唯一)来扩展你的Review类,并将NSMutableArray存储为可变形.
//Review: @interface Review : NSManagedObject @property (nonatomic,strong) NSNumber *reviewID; @end //Restaurant.h: @interface Restaurant : NSManagedObject @property (nonatomic,strong) NSSet *user_reviews; // Set of NSNumbers - (void)addUser_reviewsObject:(Review *)value; - (void)addUser_reviewsID:(NSNumber *)value; - (void)removeUser_reviewsObject:(Review *)value; - (void)addUser_reviews:(NSMutableArray *)value; - (void)removeUser_reviews:(NSMutableArray *)value; @end //Restaurant.m: - (void)addUser_reviewsObject:(Review *)value { [self addUser_reviewsID:value.reviewID]; } - (void)addUser_reviewsID:(NSNumber *)value { if(![self.user_reviews containsObject:value]; [self.user_reviews addObject:value]; } - (void)removeUser_reviewsObject:(NSNumber *)value { // follow upper logic and implement yourself } - (void)addUser_reviews:(NSMutableArray *)value { // follow upper logic and implement yourself } - (void)removeUser_reviews:(NSMutableArray *)value { // follow upper logic and implement yourself }