How to get one more datapoint beyond the requested data-range in core-data?
I'm working on an iPhone app which graphs a large database, accessed through core-data, in a line-chart. I'm using a tile-based approach to rendering this graph. The distance between datapoints is irregular.
Each tile uses a predicate to retrieve the data that is relevant for that tile:
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"creationDate > %@ AND creationDate < %@",ti开发者_运维技巧leBeginDate, tileEndDate];
[request setPredicate:predicate];
NSMutableArray *result = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
I have a problem when drawing the lines between datapoints residing in different tiles, since the drawing of this line requires a datapoint outside of the range of the tile.
Ideally, I would want to be able to get one datapoint beyond the requested range in core data. Is there any way to do this? If not, any other suggestions?
One of those classical cases where writing down the problem actually solves it. In this case unfortunately some minutes after actually posting it. Sorry.
The solution is simple: I just add two more predicates, one to retrieve the datapoint before and one to retrieve the datapoint after the requested data-range, and then I merge the arrays together.
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];
精彩评论