Search code examples
objective-ccore-datansfetchrequest

NSFetchRequest.fetchOffset broke after setting NSManagedObject's property


I wrote a method to randomly fetch record from database, but I often got the same record no matter what NSFetchRequest.fetchOffset is.

So I did an experiment, here is my code:

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

With above code, log is like:

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

which was perfectly fine.

But when I uncommented this line:

// card.phEn= @"phonetic";

log became:

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

It seems no matter what fetchOffset is, it always return the same object.


Solution

  • This could be caused by the fact that your context has unsaved (pending) changed. Unfortunately, this is not really well documented.

    You can try to set includesPendingChanges to NO for your request.