我刚刚开始学习如何在 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 数组实现相同的问题。
目前我的插入代码如下:
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
而不是 NSMutableArray
并将其与餐厅实体连接。
回顾中:
在餐厅:
如果您让 xcode 生成您的类,它将如下所示:
Restaurant:
@interface Restaurant : NSManagedObject
@property (nonatomic, strong) NSString *name;
@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) NSString *rating;
@property (nonatomic, strong) NSString *review_text;
@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
,它就不能是一个关系。
这些总是 NSSets
(如果 x 到 n)或目标类。使用 NSMutableArray
可以利用集合和自动处理的优势。
但如果你真的想存储在 NSMutableArray
中,我建议至少通过 reviewID
属性(唯一)扩展你的 Review
类并将 NSMutableArray
存储为 Transformable
。
//Review:
@interface Review : NSManagedObject
@property (nonatomic, strong) NSString *rating;
@property (nonatomic, strong) NSString *review_text;
@property (nonatomic, strong) User *user;
@property (nonatomic, strong) NSNumber *reviewID;
@end
//Restaurant.h:
@interface Restaurant : NSManagedObject
@property (nonatomic, strong) NSString *name;
@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
}
关于ios - 核心数据 : Insertion into One to Many Relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17018634/