13

我刚刚开始学习如何在 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 检索 Restaurant 及其评论的数据,并将它们存储在临时类中,然后将它们保存到 CORE 数据库。如何插入具有一对多关系的此类数据?

编辑:目前我以类的形式接收数据,该类将 user_review 属性定义为

NSMutableArray *user_reviews;

我正在尝试实现同一个类以插入核心数据模型。但核心数据模型使用 NSSet 而不是 NSMutableArray。一种粗暴的方法是复制所有具有相同属性的类,除了不使用 NSMutableArray,我将其更改为 NSSet。但这会产生大量的冗余代码。难道不应该有更有效的方法来做到这一点吗?

4

2 回答 2

21

如果您的评论应该是您的 coreDataModel 中的关系,请使用而不是并将其与餐厅实体连接NSMutableArrayNSSet

回顾: 在此处输入图像描述

在餐厅: 在此处输入图像描述

如果您让 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

编辑

如果它必须是 a NSMutableArray,它就不能是一个 ration。
那些总是NSSets(如果 x 到 n)或目标类。使用NSMutableArray利用了集合和自动处理的优点。

但是如果你真的想存储,我建议至少通过一个属性(唯一)NSMutableArray扩展你的类并将存储为ReviewreviewIDNSMutableArrayTransformable

//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
}
于 2013-06-10T07:28:58.743 回答
1
Review *review = (Review *)[NSEntityDescription insertNewObjectForEntityForName:@"Review" inManagedObjectContext:[self managedObjectContext]];
review.review_text = @"test text";
//set other properties if needed

rest.user_reviews = [NSSet setWithObjects:review, nil];//just make NSSet for you reviews
于 2013-06-10T07:21:05.773 回答