gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:32:58 25 4
gpt4 key购买 nike

在我的应用程序中,实体 SurveySurveyAction 之间存在对多关系。我正在使用 Survey code> 在多个 View Controller (步骤 1、步骤 2 等)中,每个步骤我都有多个 SurveyAction 进行调查。但是当我检索记录时,我得到了 SurveyAction我只插入最后。即,如果我在步骤 1 和步骤 2 中的记录 3 上插入两个操作,则我只得到在步骤 2 中插入的 3 个记录。这是我的数据模型-

 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 记录。

最佳答案

用这一行

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

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

[survey addActions:actionsSet]

相反

关于ios - 核心数据使用一对多关系保存记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26671582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com