gpt4 book ai didi

iOS,每次保存上下文时,核心数据都会将新对象添加到错误的实体中?

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

DataModel

我是核心数据新手。在这里,我试图在一个月内保存一堆语句。但我不知道如何在具有关系的核心数据中插入对象。我的代码正在做什么,它在 Month 实体中保存月份,并在 Statement 实体中保存语句,没有任何关系。每次我保存它时,都会在 Month 实体中创建新对象。我的数据模型有一对多关系。例如,当前(月份)11 月,我想插入一些与 11 月相关的声明。比下个月的 12 月,我想插入一些与 12 月相关的声明。这就是我想要实现的目标。抱歉我的英语不好。
`

NSManagedObjectContext *context = [self managedObjectContext];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Month" inManagedObjectContext:context];
Month *month = [[Month alloc] initWithEntity:entity insertIntoManagedObjectContext:context];

Statement *statement = [NSEntityDescription insertNewObjectForEntityForName:@"Statement" inManagedObjectContext:context];
statement.title = self.enterTextGreenViewTextField.text;

NSNumber *aNum = [NSNumber numberWithDouble:savingAmount];
statement.amount =aNum;
statement.isIncome = [NSNumber numberWithBool:isIncome];

[month addStatementsObject:statement];

NSError *error;
if (![context save:&error]) {
FATAL_CORE_DATA_ERROR(error);
return;
}

NSLog(@"savingStatement ****");

`

#import "Month.h"

NS_ASSUME_NONNULL_BEGIN

@interface Month (CoreDataProperties)

@property (nullable, nonatomic, retain) NSDate *monthOfStatement;
@property (nullable, nonatomic, retain) NSSet<Statement *> *statements;

@end

@interface Month (CoreDataGeneratedAccessors)

- (void)addStatementsObject:(Statement *)value;
- (void)removeStatementsObject:(Statement *)value;
- (void)addStatements:(NSSet<Statement *> *)values;
- (void)removeStatements:(NSSet<Statement *> *)values;

@end

NS_ASSUME_NONNULL_END

#import "Statement.h"

NS_ASSUME_NONNULL_BEGIN

@interface Statement (CoreDataProperties)

@property (nullable, nonatomic, retain) NSNumber *amount;
@property (nullable, nonatomic, retain) NSNumber *isIncome;
@property (nullable, nonatomic, retain) NSString *title;
@property (nullable, nonatomic, retain) Month *month;

@end

NS_ASSUME_NONNULL_END

最佳答案

几乎正确,您必须创建两个实体,然后绑定(bind)它们的关系:

NSEntityDescription *month = [NSEntityDescription insertNewObjectForEntityForName:@"Month"  inManagedObjectContext:[self managedObjectContext]];
Statement *statement = [NSEntityDescription insertNewObjectForEntityForName:@"Statement" inManagedObjectContext:context];

statement.title = self.enterTextGreenViewTextField.text;

NSNumber *aNum = [NSNumber numberWithDouble:savingAmount];
statement.amount =aNum;
statement.isIncome = [NSNumber numberWithBool:isIncome];

statement.month=month;

这一行我不知道是否需要手动生成集合,但我还是这样做了:

NSMutableSet *monthStatements;
if(month.statements){
monthStatements=[NSMutableSet setWithSet:month.statements];
}else{
monthStatements=[NSMutableSet alloc] init];
}
[monthStatements addObject:statement];
month.statements = monthStatements;

最后保存:

NSError *error=nil;
if (![context save:&error]) {
FATAL_CORE_DATA_ERROR(error);
return;
}

在我看来,这应该很有魅力(虽然还没有尝试过):-)

** 编辑:

获取:

    NSArray *fetchedObjects;
NSError *error=nil;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Store" inManagedObjectContext:[self managedObjectContext]];

[fetchRequest setEntity: entity];

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"code==%@", code];

[fetchRequest setPredicate:predicate];

fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error];

如果月份已经存在,您通常会尝试获取该月份,如果不存在,您可以使用我给您的代码创建它。我不知道你所说的建立新关系是什么意思......

关于iOS,每次保存上下文时,核心数据都会将新对象添加到错误的实体中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336983/

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