如何在核心数据中获取超出请求数据范围的另一个数据点?

发布于 2024-09-27 10:40:33 字数 538 浏览 5 评论 0原文

我正在开发一个 iPhone 应用程序,它以折线图的形式绘制一个大型数据库,通过核心数据访问。我正在使用基于图块的方法来渲染该图。数据点之间的距离是不规则的。

每个图块使用谓词来检索与该图块相关的数据:

NSPredicate* predicate = [NSPredicate predicateWithFormat:@"creationDate > %@ AND CreationDate < %@",tileBeginDate,tileEndDate];

[请求 setPredicate:predicate];

NSMutableArray *结果 = [[managementObjectContextexecuteFetchRequest:请求错误:&错误] mutableCopy];

在绘制驻留在不同图块中的数据点之间的线时遇到问题,因为绘制该线需要图块范围之外的数据点。

理想情况下,我希望能够获得超出核心数据请求范围的一个数据点。有什么办法可以做到这一点吗?如果没有,还有其他建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2024-10-04 10:40:33

写下问题实际上可以解决问题的经典案例之一。在这种情况下,不幸的是在实际发布几分钟后。对不起。

解决方案很简单:我只需添加两个谓词,一个用于检索请求的数据范围之前的数据点,一个用于检索请求的数据范围之后的数据点,然后将数组合并在一起。

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文