核心数据使用多对多关系保存记录?

时间:2014-10-31 09:44:55

标签: ios objective-c core-data

在我的应用中,实体to-manySurvey之间的SurveyAction关系。我在多个视图控制器中使用Survey(步骤1,步骤2等)在每个步骤中我SurveyAction多个survey。但是当我检索记录时,我得到了我最后插入的SurveyAction。即如果我在步骤1中插入两个动作,在步骤2中插入3个记录,我只获得了在步骤2中插入的三个记录。 这是我的数据模型 -

 Survey.h
    #import <Foundation/Foundation.h>
    #import <CoreData/CoreData.h>
    @class SurveyAction;
    @interface Survey : NSManagedObject
    @property (nonatomic, retain) NSSet *actions;
    //some other properies of survey object
    @end
    @interface Survey (CoreDataGeneratedAccessors)
    - (void)addActionsObject:(SurveyAction *)value;
    - (void)removeActionsObject:(SurveyAction *)value;
    - (void)addActions:(NSSet *)values;
    - (void)removeActions:(NSSet *)values;
    @end 

SurveyAction.h
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@class Survey;
@interface SurveyAction : NSManagedObject
@property (nonatomic, retain) NSString * action;
@property (nonatomic, retain) NSString * deadline;
@property (nonatomic, retain) NSString * responsible;
@property (nonatomic, retain) NSString * surveyId;
@property (nonatomic, retain) NSString * step;
@property (nonatomic, retain) Survey *survey;
@end

我在这里做错了什么?任何帮助或建议将不胜感激。

编辑: - 我可以有多个记录(调查),我有调查列表,当我选择一行时,我将选定的调查传递给下一个VC。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Survey* survey = [listData objectAtIndex:indexPath.row];
    ParticularsViewController* vc = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"particularsView"];
    vc.survey = survey;
    [self.navigationController pushViewController:vc animated:YES];
}

并在下一个VC中确保我得到正确的调查我使用谓词并保存我的数据 -

-(void)rightbuttonPressed
{
 Survey* survey;
    NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
    [fetchRequest setEntity:[NSEntityDescription entityForName:@"Survey"
                                        inManagedObjectContext:self.managedObjectContext]];
    [fetchRequest setFetchLimit:1];

    // check whether the entity exists or not
    [fetchRequest setPredicate:[NSPredicate predicateWithFormat: @"(SELF = %@)", self.survey]];

    // if get a entity, that means exists, so fetch it.
    if ([self.managedObjectContext countForFetchRequest:fetchRequest error:&error])
        survey = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] lastObject];

    // if not exists, just insert a new entity
    else survey = [NSEntityDescription insertNewObjectForEntityForName:@"Survey"
                                                inManagedObjectContext:self.managedObjectContext];
    //surveyAction
    if (actionsArray.count > 0) {

        NSMutableSet* actionsSet = [[NSMutableSet alloc] init];
        for (Action* obj in actionsArray) {

            SurveyAction *actionDescription = [NSEntityDescription insertNewObjectForEntityForName:@"SurveyAction"
                                                                            inManagedObjectContext:self.managedObjectContext];

            [actionDescription setValue:@"1" forKey:@"step"];
            [actionDescription setValue:obj.action forKey:@"action"];
            [actionDescription setValue:obj.person forKey:@"responsible"];
            [actionDescription setValue:obj.deadline forKey:@"deadline"];

            [actionsSet addObject:actionDescription];
        }
        [survey setValue:actionsSet forKey:@"actions"];
       // survey.actions = actionsSet;
    }
    if (! [self.managedObjectContext save:&error])
        NSLog(@"Couldn't save data to %@", NSStringFromClass([self class]));
}

这就是我获取记录的方式

-(void)populateView
{
 int counter = 0;
    for (SurveyAction *sAction in self.survey.actions) {
        if ([sAction.step isEqualToString:@"1"]) {

            Action* action = [[Action alloc] initWithObject:sAction];
            [self addrowView:action withframeorigin:198+(counter*40)];
            counter++;
        }

    }
} 

但正如我所说,我只获得了最后添加的SurveyAction条记录。

1 个答案:

答案 0 :(得分:1)

使用此行

        [survey setValue:actionsSet forKey:@"actions"];
       // survey.actions = actionsSet;

您正在替换现有操作,而不是添加它们。使用

[survey addActions:actionsSet]

相反