データベースからランダムにレコードを取得するメソッドを書いたのですが、NSFetchRequest.fetchOffset が何であっても同じレコードを取得することがよくありました。
だから私は実験をしました、ここに私のコードがあります:
- (Card *)getNextCard {
Card *card = nil;
while (YES) {
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Card"];
request.fetchLimit = 1;
request.fetchOffset = arc4random_uniform(1031);
NSError *error = nil;
NSArray *matches = [self.database.managedObjectContext executeFetchRequest:request error:&error];
if (matches) {
card = [matches lastObject];
// card.phEn= @"phonetic";
}
NSLog(@"nextCard: %@, %d", card ? card.en : card, request.fetchOffset);
}
return card;
}
上記のコードでは、ログは次のようになります。
2013-05-07 23:11:16.035 MyApp[20598:707] nextCard: astound, 153
2013-05-07 23:11:17.175 MyApp[20598:707] nextCard: complicate, 370
2013-05-07 23:11:18.335 MyApp[20598:707] nextCard: hasten, 954
2013-05-07 23:11:19.801 MyApp[20598:707] nextCard: exonerate, 749
2013-05-07 23:11:23.159 MyApp[20598:707] nextCard: exalted, 736
2013-05-07 23:11:24.389 MyApp[20598:707] nextCard: Leninism, 3
2013-05-07 23:11:25.749 MyApp[20598:707] nextCard: commotion, 360
2013-05-07 23:11:27.959 MyApp[20598:707] nextCard: discern, 565
2013-05-07 23:11:28.895 MyApp[20598:707] nextCard: aluminum, 85
これは完全に問題ありませんでした。
しかし、この行のコメントを外したとき:
// card.phEn= @"ふりがな";
ログは次のようになりました:
2013-05-07 23:25:02.183 MyApp[20645:707] nextCard: annex, 104
2013-05-07 23:25:04.042 MyApp[20645:707] nextCard: annex, 614
2013-05-07 23:25:06.095 MyApp[20645:707] nextCard: annex, 926
2013-05-07 23:25:07.393 MyApp[20645:707] nextCard: annex, 628
2013-05-07 23:25:08.968 MyApp[20645:707] nextCard: annex, 809
2013-05-07 23:25:10.316 MyApp[20645:707] nextCard: annex, 265
2013-05-07 23:25:11.534 MyApp[20645:707] nextCard: annex, 762
fetchOffset が何であれ、常に同じオブジェクトを返すようです。