代码之家  ›  专栏  ›  技术社区  ›  TinkerTank

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

  •  1
  • TinkerTank  · 技术社区  · 14 年前

    每个磁贴使用谓词检索与该磁贴相关的数据:

    [请求集合谓词:谓词];

    NSMutableArray*result=[[managedObjectContext executeFetchRequest:请求错误:&错误]mutableCopy];

    我在绘制位于不同分幅中的数据点之间的线时遇到问题,因为绘制此线需要分幅范围之外的数据点。

    1 回复  |  直到 14 年前
        1
  •  0
  •   TinkerTank    14 年前

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

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

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