开发者

app crashing due to not storing data in database

开发者 https://www.devze.com 2023-04-11 03:33 出处:网络
I dont know where m getting wrong here is the output of console 2011-10-07 11:03:29.508 Golden Corral[2365:207] After whole process Name : Ggggggggggg

I dont know where m getting wrong here is the output of console

2011-10-07 11:03:29.508 Golden Corral[2365:207] After whole process Name : Ggggggggggg
2011-10-07 11:03:29.513 Golden Corral[2365:207] After whole process Score : 27600
2011-10-07 11:03:29.515 Golden Corral[2365:207] Error : Error Domain=NSCocoaErrorDomain Code=1660 "The operation couldn’t be completed. (Cocoa error 1660.)" UserInfo=0x5566d60 {NSValidationErrorObject=<HighScore: 0x5563eb0> (entity: HighScore; id: 0x5555ed0 <x-coredata:///HighScore/tDAD877F0-0594-4E14-819F-AF5BDA4A38A82> ; data: {
PlayerName = Ggggggggggg;
TopTenScore = 27600;
}), NSValidationErrorKey=PlayerName, NSLocalizedDescription=The operation couldn’t be completed. (Cocoa error 1660.), NSValidationErrorValue=Ggggggggggg}

and here is my code of saving data. I am checking whether the user enteres his name for highscore or not, if not m taking name Anonymous else what the user has entered. Gdb shows the correct value of both user name and highScore but data is not storing in database.

    -(void)calculateHighScore
{

    //////////////////////////       Core Data  Entries.    ///////////////


    HighScore *ScoreData = (HighScore*)[NSEntityDescription insertNewObjectForEntityForName:@"HighScore" inManagedObjectContext:self.managedObjectContext];



    if ([txtName.text isEqualToString:@""] || [txtName.text length] == 0 )
    {



            NSLog(@"The Name :%@",txtName.text);

            NSLog(@"%d",CountHighScore);

            ScoreData.PlayerName = @"Anonymous";
            ScoreData.TopTenScore = [NSNumber numberWithInt:CountHighScore];

            NSLog(@"The Name :%@",ScoreData.PlayerName);

            NSLog(@"%d",CountHighScore);

            isWinner = NO;
            levelCount ++;




    }

    else 
    {



            NSLog(@"Befor Text assign highscore :%d",CountHighScore);

            //CountHighScore = [txtName.text intValue];

            ScoreData.PlayerName = txtName.text;
            ScoreData.TopTenScore = [NSNumber numberWithInt:CountHighScore];

            NSLog(@"The Name :%@",txtName.text);

            NSLog(@"%d",CountHighScore);


            isWinner = NO;
            levelCount ++;




    }

    NSLog(@"After whole process Name : %@",ScoreData.PlayerName);
    NSLog(@"After whole process Score : 开发者_高级运维%d",CountHighScore);

    // Code for inserting into DataBase
    NSError *CorrectError;      
    if (![self.managedObjectContext save:&CorrectError])
    {
        // Handle the error...
        NSLog(@"Error : %@",CorrectError);
    }



}


Answer was found during conversation in comments.

When you are creating new key in your model you can set restrictions to the values of that property.

In current example restrictions were set on values of key PlayerName: maximum length of it was set to 8.

So when you set value of PlayerName key to @"Ggggggggggg" that has length more then 8 you will receive error when committing changes to the database. All restrictions are checked only after your have made all the changes to objects and want to save them to your database by committing changes.

0

精彩评论

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