核心数据:插入一对多关系

时间:2013-06-10 07:16:36

标签: ios objective-c cocoa-touch core-data relational-database

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

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

NSMutableArray *user_reviews;

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

2 个答案:

答案 0 :(得分:21)

如果您的评论是您的coreDataModel中的关系,请使用NSMutableArray代替NSSet并将其与餐馆实体联系起来。

回顾: enter image description here

在餐厅: enter image description here

如果你让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中,我建议您至少按Review属性(唯一)展开您的reviewID课程,并将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
}

答案 1 :(得分: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