我是不是正确地将对象添加到核心数据?

Posted

技术标签:

【中文标题】我是不是正确地将对象添加到核心数据?【英文标题】:Am I Correctly Adding An Object To Core Data?我是否正确地将对象添加到核心数据? 【发布时间】:2011-04-27 05:16:06 【问题描述】:

我有一个实体“Routine”,它与“Exercise”有一对多的关系,称为“routineExercises”。

我有一个名为 addExercise 的方法,它应该将练习添加到例程的实例中。

这是在代码中实现的正确方法吗?

Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

exercise.name = selectedExercise;

NSMutableSet *exercises = [[NSMutableSet alloc]init];

[exercises addObject:exercise];

Routine *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

routine.routineExercises = exercises;

更新:我在routine.routineExercises = 练习中遇到错误,显示“-[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x711bf30 2011-04-27 01:25:37.683 Curl[888:207] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[Routine setRoutineExercises:]:无法识别的选择器发送到实例 0x711bf30” "

更新:

-(void)addExercise

   if (managedObjectContext == nil) 
     
        managedObjectContext = [(CurlAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
        [managedObjectContext retain];
    

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Exercise" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) 
        // Handle the error.
    

    Exercise *exercise = (Exercise *)[NSEntityDescription insertNewObjectForEntityForName:@"Exercise" inManagedObjectContext:managedObjectContext];

    exercise.name = selectedExercise;

    NSMutableSet *exercises = [NSSet setWithObjects:exercise,nil];

    Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

    routine.routineExercises = exercises;

    if (![managedObjectContext save:&error]) 
    
        // Handle the error.
    
    NSLog(@"%@", error);

    [self.routineTableView reloadData];
    [mutableFetchResults release];
    [request release];

错误:

2011-04-27 03:05:58.902 Curl[1954:207] -[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x5d4a4e0
2011-04-27 03:05:58.904 Curl[1954:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Routine setRoutineExercises:]: unrecognized selector sent to instance 0x5d4a4e0'

例程.h

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

@class Exercise;

@interface Routine : NSManagedObject 
@private

@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSDate * timeStamp;
@property (nonatomic, retain) NSSet* routineExercises;

@end

例程.m

#import "Routine.h"
#import "Exercise.h"


@implementation Routine
@dynamic name;
@dynamic timeStamp;
@dynamic routineExercises;

- (void)addRoutineExercisesObject:(Exercise *)value     
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"routineExercises"] addObject:value];
    [self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)removeRoutineExercisesObject:(Exercise *)value 
    NSSet *changedObjects = [[NSSet alloc] initWithObjects:&value count:1];
    [self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [[self primitiveValueForKey:@"routineExercises"] removeObject:value];
    [self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:changedObjects];
    [changedObjects release];


- (void)addRoutineExercises:(NSSet *)value     
    [self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"routineExercises"] unionSet:value];
    [self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueUnionSetMutation usingObjects:value];


- (void)removeRoutineExercises:(NSSet *)value 
    [self willChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];
    [[self primitiveValueForKey:@"routineExercises"] minusSet:value];
    [self didChangeValueForKey:@"routineExercises" withSetMutation:NSKeyValueMinusSetMutation usingObjects:value];


@end

【问题讨论】:

【参考方案1】:

你错过了常规*的演员表。

改变

Routine *routine = [NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

Routine *routine = (Routine *)[NSEntityDescription insertNewObjectForEntityForName:@"Routine" inManagedObjectContext:managedObjectContext];

【讨论】:

谢谢!我想我需要添加类似NSMutableSet *exercises = [exercise mutableSetValueForKey:@"name"]; 的行? 这将为您提供对无序对多关系的读写访问权限,并为您提供所需的相关/非相关。您也可以使用NSSet *exercises = [NSSet setWithObjects:exercise,nil];,然后保持routine.routineExercises = exercises; 不变。现在不保存了吗? 我使用了你刚刚添加的代码,但现在我在exercises addObject:exercise 收到错误消息,上面写着 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFSet addObject:]:发送到不可变对象的变异方法'。我认为在实体类中,关系是 NSSet 而不是 NSMutableSet。 如果您使用我的代码,则无需调用[exercises addObject:exercise];,因为在实例化时已经将练习对象添加到练习 NSSet 中。 Here's the CoreData reference 向下滚动到多对多关系部分了解更多信息。

以上是关于我是不是正确地将对象添加到核心数据?的主要内容,如果未能解决你的问题,请参考以下文章

动态地将组合框单元添加到 datagridview

CoreData 一次又一次地将数据添加到数据库中

如何正确地将多个片段添加到片段过渡?

如何正确地将 UITextField 添加到 UITableViewCell

如何正确地将安全区域插图添加到 UIStackView?

如何正确地将 Kal 框架添加到我的 iPhone 项目中?