views:

32

answers:

2

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?

A: 

What does the Console report if you add NSLog(@"qst: %@:, qst); immediately after the qst instance's -insertNewObjectForEntityName:inManagedObjectContext: call?

What does the Console report if you add NSLog(@"answer: %@:, answer); immediately after the answer instance's -insertNewObjectForEntityName:inManagedObjectContext: call?

Alex Reynolds
added what I is reported...
Junior B.
A: 

Here what is reported with NSLog(@"qst: %@", qst);

2010-05-24 23:37:33.948 FGQ[452:207] qst: (entity: Question; id: 0x3c19ab0 ; data: { answers = ( ); package = nil; questionText = nil; })

Junior B.