2013-04-13 11 views
6

ここで間違っていることがわかりません。CoreData to-manyエラーを追加する

学校には学生に多対多があり、学生にはその逆があります。 enter image description here


enter image description here


少しテストコードは、次のように

@class Student; 

@interface School : NSManagedObject 

@property (nonatomic, retain) NSString * name; 
@property (nonatomic, retain) NSOrderedSet *students; 
@end 

@interface School (CoreDataGeneratedAccessors) 

- (void)insertObject:(Student *)value inStudentsAtIndex:(NSUInteger)idx; 
- (void)removeObjectFromStudentsAtIndex:(NSUInteger)idx; 
- (void)insertStudents:(NSArray *)value atIndexes:(NSIndexSet *)indexes; 
- (void)removeStudentsAtIndexes:(NSIndexSet *)indexes; 
- (void)replaceObjectInStudentsAtIndex:(NSUInteger)idx withObject:(Student *)value; 
- (void)replaceStudentsAtIndexes:(NSIndexSet *)indexes withStudents:(NSArray *)values; 
- (void)addStudentsObject:(Student *)value; 
- (void)removeStudentsObject:(Student *)value; 
- (void)addStudents:(NSOrderedSet *)values; 
- (void)removeStudents:(NSOrderedSet *)values; 
@end 



// Meanwhile, elsewhere... 
-(void)go { 
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
    NSManagedObjectContext *context = [app managedObjectContext]; 

    School *school = (School *)[NSEntityDescription insertNewObjectForEntityForName:@"School" inManagedObjectContext:context]; 
    [school setName:@"Stanford"]; 

    Student *student = (Student *)[NSEntityDescription insertNewObjectForEntityForName:@"Student" inManagedObjectContext:context]; 
    [student setName:@"Eric"]; 


    //[school addStudentsObject:student]; 

    NSMutableSet *students = [school mutableSetValueForKey:@"students"]; 
    [students addObject:student]; 


    NSError *__autoreleasing error; 
    BOOL success = [context save:&error]; 

    if (!success) { 
     @throw [NSException exceptionWithName:NSGenericException 
            reason:[error description] 
            userInfo:nil]; 
    } 
} 

がコメントaddStudentsObject:を使用して失敗した:

を使用
2013-04-13 16:22:58.648 CDTMTest[2098:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSSet intersectsSet:]: set argument is not an NSSet' 
*** First throw call stack: 
(0x1fa2012 0x13dfe7e 0x2030a4a 0xb85ec6 0xb087f9 0xb85d33 0x11aa638 0x7ee2069 0x2b4d 0xacd5b3 0x1f61376 0x1f60e06 0x1f48a82 0x1f47f44 0x1f47e1b 0x1efc7e3 0x1efc668 0x13ffc 0x1bdd 0x1b05) 
libc++abi.dylib: terminate called throwing an exception 

は、私は同様の問題があった

2013-04-13 16:07:05.111 CDTMTest[2012:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSManagedObjects of entity 'School' do not support -mutableSetValueForKey: for the property 'students'' 
*** First throw call stack: 
(0x1fa3012 0x13e0e7e 0x11370da 0x3af3 0xace5b3 0x1f62376 0x1f61e06 0x1f49a82 0x1f48f44 0x1f48e1b 0x1efd7e3 0x1efd668 0x14ffc 0x2b7d 0x2aa5) 
libc++abi.dylib: terminate called throwing an exception 
+0

これは単なるタイプミスかもしれないが、私は混乱している:あなたが言及:「コメント 'setStudentsObject'を」障害の原因となっています。しかし、あなたの質問のテキストでは、コメント行は 'addStudentsObject'です。それはちょうどあなたの質問の誤植ですか、それともエラーの原因ですか? –

+0

@Anthony:それはタイプミスです。 'add〜'は正しい、修正されています。ありがとう! – QED

+3

[NSOrderedSetで生成されたアクセサでスローされた例外]の複製が可能です(0120-912-03)。 –

答えて

8

あなたがセットに追加するためにこれをしようとする場合があります:あなたが、多くの関係を、あなたを選んだとして

mutableOrderedSetValueForKey: 

注文する。 しかし、私はあなただけの関係に他の方法で設定することが容易になりますことを信じて:

student.school = school; 
+0

これは私が当面していることであり、注文を正しく維持しています。しかし、私はなぜaddメソッドがうまくいかないのか、なぜこれがまだここに現れていないのか分かりません。それは私に何かが足りないと思うようにする。 – QED

+1

これはすでにポップアップしています:http:// stackoverflow。com/questions/7385439/exception-throws-in-nsorderedset-generated-accessorsを参照してください。 –

2

で失敗します。多対一関係の基本的なコアデータの実装が常に正しく動作していないことをどこかで読んでいます。この関係には、nssetを受け付ける関係と、生成されたsetterが入ってくる引数をnssetに適切に変換しないという関係があります。それを修正するには、addStudentsObjectをオーバーライドしてみてください:このような何かを持つ方法:

static NSString *const kItemsKey = @"students"; 

- (void)addStudentsObject:(Student *)value { 
    NSMutableSet* tempSet = [NSMutableSet setWithSet:self.students]; 
    NSSet* newObject = [NSSet setWithObject:value]; 
    [self willChangeValueForKey:kItemsKey withSetMutation:NSKeyValueUnionSetMutation usingObjects:newObject]; 
    [tempSet addObject:value]; 
    self.students = tempSet; 
    [self didChangeValueForKey:kItemsKey withSetMutation:NSKeyValueUnionSetMutation usingObjects:newObject]; 
} 
+0

私は自由な瞬間があるときにこれを試してみましょう。あなたがそれを読んでいる場所を知ることができれば、リンクを投稿してください。コードをありがとう! – QED

+0

実際には、それを考えに来て - それはCoreDataドキュメントにあります... – QED

+0

本当に*順序付けされた関係が必要ですか?順序付けられていない関係では問題は発生しません。 –

関連する問題