2013-04-13 18 views
6

Nie jestem pewien, co robię źle tutaj.Błąd dodania wielu-do-wielu CoreData

Szkoła ma wiele do Ucznia, a Uczeń ma odwrotność. enter image description here


enter image description here


Trochę kodu testu w następujący sposób:

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

Używanie skomentował addStudentsObject: nie powiedzie się z:

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 

Korzystanie mutableSetValueForKey: nie z

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

To może być tylko literówka, ale jestem zdezorientowany: można wymienić: „komentowanej' setStudentsObject'” powoduje awarię. Ale w tekście twojego pytania skomentowana linia to 'addStudentsObject'. Czy to tylko literówka w pytaniu, czy też to, co powoduje błąd? –

+0

@Anthony: to literówka. 'add ~' jest poprawne, naprawione. Dzięki! – QED

+3

możliwy duplikat [Wyjątek zgłoszony w akcesoriach generowanych przez NSOrderedSet] (http://stackoverflow.com/questions/7385439/exception-thrown-in-nsorderedset-generated-accessors) –

Odpowiedz

8

może chcesz spróbować to na dodanie do zestawu:

mutableOrderedSetValueForKey: 

Jak was wybrałem do wielu relacji do być zamówionym. ale wierzę, że będzie Ci łatwiej po prostu ustawić zależność w drugą stronę:

student.school = school; 
+0

To jest to, co robię na razie i utrzymuje porządek w porządku. Ale nadal nie wiem, dlaczego metoda dodawania nie zadziała, lub dlaczego nie pojawiła się tutaj już na SO. To sprawia, że ​​myślę, że czegoś brakuje. – QED

+1

To już pojawiło się: http: // stackoverflow.com/questions/7385439/exception-throw-in-nsorderedset-generated-accessors. –

2

miałem podobny problem. Czytałem gdzieś, że podstawowa implementacja danych dla relacji wiele-wiele nie działa poprawnie przez cały czas. Ma to coś wspólnego z relacją akceptującą tylko nsset i wygenerowane setery niepoprawnie konwertujące przychodzące argumenty do nsset. Aby rozwiązać to spróbuj przesłanianie addStudentsObject: metodę z coś takiego:

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

Dam to spróbować, gdy mam wolną chwilę. Jeśli wiesz, gdzie to jest, przeczytaj proszę. Dzięki za kod! – QED

+0

Właściwie, pomyśl o tym - to jest w dokumentach CoreData ... – QED

+0

Czy naprawdę potrzebujesz * uporządkowanej * relacji? Problem nie występuje w przypadku nieuporządkowanych relacji. –

Powiązane problemy