iOS,Core Data每次保存上下文时都会向错误的实体添加新对象?

时间:2015-10-26 01:07:01

标签: ios core-data

DataModel

我对核心数据不熟悉。在这里,我试图在一个月内保存一堆Statement。但我不知道如何在关系中插入核心数据中的对象。我的代码正在做什么,它在Month实体和Statement实体中的语句中保存了一个月没有任何关系。每次我保存它在Month实体中创建新对象。我的DataModel有一对多的关系。例如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

1 个答案:

答案 0 :(得分:1)

几乎是正确的,你必须创建两个实体,然后绑定它们的关系:

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];

您通常会尝试抓取月份,以防它已经存在,如果它没有用我给你的代码创建它。我不知道你通过建立新的关系来了解你的意思......