我在 coredata 中插入了一些对象,但是当我检索它们时,它们的顺序与我插入的不同,甚至索引也得到了适当的管理,
-(void)addQuestion
{
NSManagedObjectContext *context = [self managedObjectContext];
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObject:@"What is capital of Australia?"];
[array addObject:@"What is capital of Canada?"];
[array addObject:@"What is capital of UAE?"];
[array addObject:@"What is capital of USA?"];
[array addObject:@"What is capital of Pakistan?"];
[array addObject:@"What is capital of China?"];
[array addObject:@"What is capital of UK?"];
[array addObject:@"What is capital of Russia?"];
[array addObject:@"What is capital of Romania?"];
[array addObject:@"What is capital of Germany?"];
for (int x = 0; x<10; x++) {
Question *question = [NSEntityDescription
insertNewObjectForEntityForName:@"Question"
inManagedObjectContext:context];
question.question = [array objectAtIndex:x];
question.answer = @"Testville";
question.mcqsa = @"Sydney";
question.mcqsb = @"Canbera";
question.mcqsc = @"Berlin";
question.mcqsd = @"Bern";
}
NSError *error;
if (![context save:&error]) {
NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}
// Test listing all FailedBankInfos from the store
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Question"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Question *info in fetchedObjects) {
NSLog(@"Name: %@", info.question);
}
}
日志显示
2013-06-04 17:59:59.978 iQuiz[972:c07] Name: What is capital of Romania?
2013-06-04 17:59:59.979 iQuiz[972:c07] Name: What is capital of Australia?
2013-06-04 17:59:59.979 iQuiz[972:c07] Name: What is capital of Pakistan?
2013-06-04 17:59:59.980 iQuiz[972:c07] Name: What is capital of Russia?
2013-06-04 17:59:59.980 iQuiz[972:c07] Name: What is capital of USA?
2013-06-04 17:59:59.981 iQuiz[972:c07] Name: What is capital of UK?
2013-06-04 17:59:59.981 iQuiz[972:c07] Name: What is capital of UAE?
2013-06-04 17:59:59.982 iQuiz[972:c07] Name: What is capital of Germany?
2013-06-04 17:59:59.982 iQuiz[972:c07] Name: What is capital of China?
2013-06-04 17:59:59.982 iQuiz[972:c07] Name: What is capital of Canada?