开发者

Why is my app crashing when I add a new managed object to a relationship set?

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?


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?


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; })

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜