問題を書き留めることで実際に解決する古典的なケースの1つ。この場合、残念ながら実際に投稿してから数分後です。ごめん。
解決策は簡単です。2つの述語を追加するだけです。1つは要求されたデータ範囲の前のデータポイントを取得し、もう1つは要求されたデータ範囲の後にデータポイントを取得します。次に配列をマージします。
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
// Get the the last point (closest to now) first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[request setFetchLimit:1]; // Keeps the application fast and the memory requirements low.
[sortDescriptors release];
[sortDescriptor release];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"creationDate > %@",realEndDate];
[request setPredicate:predicate];
NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
// Get the data within the requested range.
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[request setFetchLimit:200]; // Keeps the application fast and the memory requirements low.
[sortDescriptors release];
[sortDescriptor release];
predicate = [NSPredicate predicateWithFormat:@"creationDate > %@ AND creationDate < %@",beginDate, realEndDate];
[request setPredicate:predicate];
NSMutableArray *additionalFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
[mutableFetchResults addObjectsFromArray:additionalFetchResults];
[additionalFetchResults release];
// Get the first point last
[request setFetchLimit:1];
predicate = [NSPredicate predicateWithFormat:@"creationDate < %@",beginDate];
[request setPredicate:predicate];
additionalFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
[mutableFetchResults addObjectsFromArray: additionalFetchResults];
[additionalFetchResults release];