我正在核心数据中进行测试并且有一个大约有的数组。我试图存储在我的数据库中的230条记录。不幸的是,只有最后一条记录似乎存储在数据库中。我只是无法弄清楚为什么我可以看到while循环正确循环?
此外,如果一个字段(nrField)被索引,我是否需要为其添加数字,或者这会在我向数据库添加记录时自动发生? nrFields是存储在每条记录中的唯一计数器。
//=========LOOP ARRAY AND INSERT RECORDS INTO DB==============//
while (eOF != 99999) {
qNr = [myArray objectAtIndex:xx];
eOF = [qNr intValue];
xx = xx++;
if (eOF != 99999) {
qDifficulties = [myArray objectAtIndex:xx];
int iDiff = [qDifficulties intValue];
[famQuizInfo setValue:[NSNumber numberWithInt:iDiff] forKey:@"qDiff"];
xx = xx++;
// No action for Category as it is not included in the database any longer
xx = xx++; // Calculate +1 to compensate for non existing Category
[famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"question"];
xx = xx++;
[famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qRightAnswer"];
xx = xx++;
[famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer1"];
xx = xx++;
[famQuizInfo setValue:[myArray objectAtIndex:xx] forKey:@"qWrongAnswer2"];
[famQuizInfo setValue:[NSNumber numberWithInt:0] forKey:@"qNrAccess"];
[famQuizInfo setValue:[NSDate date] forKey:@"qRegDate"];
//NSError *error;
if (![qContext save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
xx = xx++;
NSLog(@"xx: %i", xx);
}
}
答案 0 :(得分:2)
问题是您正在更新记录并在循环中覆盖它。每次循环时都需要创建一个famQuizInfo
的新实例。