views:

15

answers:

1

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 < %@",tileBeginDate, 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?

A: 

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];
TumbleCow
Mark your question as answered by yourself then.
Thomas Børlum
apparently, I can do so after two days. If this is till the only answer by then, I will.
TumbleCow