I've this simple Core Data Model:
Question, Answer
Every every question has 4 answers.
The code is the following: Question.m
@interface Question : NSManagedObject
{
}
@property (nonatomic, retain) NSString * questionText;
@property (nonatomic, retain) NSSet* answers;
@property (nonatomic, retain) Package * package;
@end
@interface Question (CoreDataGeneratedAccessors)
- (void)addAnswersObject:(NSManagedObject *)value;
- (void)removeAnswersObject:(NSManagedObject *)value;
- (void)addAnswers:(NSSet *)value;
- (void)removeAnswers:(NSSet *)value;
@end
Answer.m
@class Question;
@interface Answer : NSManagedObject
{
}
@property (nonatomic, retain) NSString * answerText;
@property (nonatomic, retain) NSNumber * correct;
@property (nonatomic, retain) Question * question;
@end
The problem is when i try to add an answer to a question with addAnswersObject.
This is the part of the code that crash the app:
for (CXMLElement *theElement in theNodes)
{
Question *qst = [NSEntityDescription insertNewObjectForEntityForName:@"Question" inManagedObjectContext:moc];
// Create a counter variable as type "int"
int counter;
// Loop through the children of the current node
for(counter = 0; counter < [theElement childCount]; counter++) {
if([[[theElement childAtIndex:counter] name] isEqualToString: @"question"])
[qst setQuestionText:[[theElement childAtIndex:counter] stringValue]];
if([[[theElement childAtIndex:counter] name] isEqualToString: @"answer"]) {
Answer *answer = [NSEntityDescription insertNewObjectForEntityForName:@"Answer" inManagedObjectContext:moc];
[answer setAnswerText:[[theElement childAtIndex:counter] stringValue]];
CXMLElement *answerElement = (CXMLElement *)[theElement childAtIndex:counter];
if([[[answerElement attributeForName:@"correct"] stringValue] isEqualToString:@"YES"]) {
[answer setCorrect:[NSNumber numberWithBool:YES]];
} else {
[answer setCorrect:[NSNumber numberWithBool:NO]];
}
[qst addAnswersObject:answer]; //The app crash here
}
}
This is the log from console:
2010-05-24 20:02:38.475 Fgq[5670:40b] * -[NSUserDefaults objectForKey:]: message sent to deallocated instance 0x3c179a0 Program received signal: “EXC_BAD_ACCESS”.
I re-exported many times all objects from the Object Data Model without success, I've checked all relationships and it seems that everything is ok.
What kind of problem could be?