我有以下问题:
设置同一实体的元素之间的关系是可行的,但 2 个不同实体之间的关系往往会出错。这是尝试保存时的错误输出:
NSLocalizedDescription = "The operation couldn\U2019t be completed. (Cocoa error 1550.)";
NSValidationErrorKey = responsible;
NSValidationErrorObject = "<Actor: 0x8876840> (entity: Actor; id: 0x8875210 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Actor/p2> ; data: {\n belongings = \"<relationship fault: 0x8a0c660 'belongings'>\";\n name = Sofie;\n orderingValue = 2;\n performsA = \"<relationship fault: 0x8a190d0 'performsA'>\";\n performsC = \"<relationship fault: 0x8a19110 'performsC'>\";\n performsI = \"<relationship fault: 0x8a19150 'performsI'>\";\n performsR = \"<relationship fault: 0x8a19190 'performsR'>\";\n responsible = (\n \"0x8884e10 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Goal/p1>\"\n );\n supervisedBy = \"<relationship fault: 0x8a191f0 'supervisedBy'>\";\n supervising = \"<relationship fault: 0x8a19230 'supervising'>\";\n type = Person;\n})";
NSValidationErrorValue = "Relationship 'responsible' on managed object (0x8876840) <Actor: 0x8876840> (entity: Actor; id: 0x8875210 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Actor/p2> ; data: {\n belongings = \"<relationship fault: 0x8a0c660 'belongings'>\";\n name = Sofie;\n orderingValue = 2;\n performsA = \"<relationship fault: 0x8a190d0 'performsA'>\";\n performsC = \"<relationship fault: 0x8a19110 'performsC'>\";\n performsI = \"<relationship fault: 0x8a19150 'performsI'>\";\n performsR = \"<relationship fault: 0x8a19190 'performsR'>\";\n responsible = (\n \"0x8884e10 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Goal/p1>\"\n );\n supervisedBy = \"<relationship fault: 0x8a191f0 'supervisedBy'>\";\n supervising = \"<relationship fault: 0x8a19230 'supervising'>\";\n type = Person;\n}) with objects {(\n <NSManagedObject: 0x8885d60> (entity: Goal; id: 0x8884e10 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Goal/p1> ; data: {\n concerning = \"<relationship fault: 0x8a1ba40 'concerning'>\";\n conflictedBy = \"<relationship fault: 0x8a1c5c0 'conflictedBy'>\";\n conflicting = \"<relationship fault: 0x8a1c600 'conflicting'>\";\n higherLevelGoalAND = \"<relationship fault: 0x8a1c640 'higherLevelGoalAND'>\";\n higherLevelGoalOR = \"<relationship fault: 0x8a1c680 'higherLevelGoalOR'>\";\n lowerLevelGoalAND = \"<relationship fault: 0x8a1c6c0 'lowerLevelGoalAND'>\";\n lowerLevelGoalOR = \"<relationship fault: 0x8a1c700 'lowerLevelGoalOR'>\";\n name = \"Customer satisfaction\";\n operationalization = \"<relationship fault: 0x8a1c740 'operationalization'>\";\n orderingValue = 1;\n responsibles = (\n \"0x8875210 <x-coredata://599B45F7-F346-4674-B1B9-5BAEBD575E74/Actor/p2>\"\n );\n})\n)}";
这是我的一些代码:
Actor.h
...
@interface Actor (CoreDataGeneratedAccessors)
- (void)addResponsibleObject:(Goal *)value;
- (void)removeResponsibleObject:(Goal *)value;
- (void)addResponsible:(NSSet *)values;
- (void)removeResponsible:(NSSet *)values;
...
ActorOverview.h
...
@interface ActorOverview : NSObject
{
NSMutableArray *allActors;
NSMutableArray *actorFetch;
NSManagedObjectContext *context;
NSManagedObjectModel *model;
}
@property (nonatomic, retain) NSManagedObjectContext *context;
...
-(void)relationshipBetweenActor:(Actor *)a andEntity:(NSManagedObject *)e withInfo:(NSString *)info action:(NSString *)action;
...
(relationshipBetweenActor:andEntity:withInfo:action: 在设置 2 个演员之间的关系时工作正常)
但是,当我执行以下操作来设置目标和演员之间的关系时,它失败了(“演员对某个目标负责”)
在 ActorZoomViewController.m 中
// Define the actor
ActorOverview *ao = [[ActorOverview alloc]init];
Actor *actorInZoom = [ao fetchActorWithOrderingValue:currentActor];
// Define the goal
GoalOverview *go = [[GoalOverview alloc]init];
NSArray *goals = [[GoalOverview defaultOverview]allGoals];
Goal *goalInZoom = [goals objectAtIndex:0];
// Adding a new relationship
[ao relationshipBetweenActor:actorInZoom andEntity:goalInZoom withInfo:@"" action:@"add"];
// Save
[[ActorOverview defaultOverview]saveChanges];
在 ActorOverview.m 中调用 relationshipBetweenActor:andEntity:withInfo:action:
-(void)relationshipBetweenActor:(Actor *)a andEntity:(NSManagedObject *)e withInfo:(NSString *)info action:(NSString *)action
{
NSEntityDescription *ed = [e entity];
if([[ed name] isEqualToString:@"Goal"])
{
if([action isEqualToString:@"add"])
{
NSLog(@"NEW relationship between Actor: %@ and Goal: %@", [a name], [e name]);
[a addResponsibleObject:e];
}
if([action isEqualToString:@"remove"])
{
NSLog(@"REMOVED relationship between Actor: %@ and Goal: %@", [a name], [e name]);
[a removeResponsibleObject:e];
}
}
}
这会导致保存时出错。
谁能给我一些建议?