0

我正在构建一个使用 Core Location 框架跟踪用户运动的健身应用程序。我正在使用 Core Data 框架保存数据。目前我有两个实体;锻炼和位置。Workout 由这些 Location 对象组成,它们的主要属性是纬度和经度。

当我尝试从这些 Location 对象创建 MKPolyLine 时,需要在设备上花费大量时间。

- (void)createRouteLineAndAddOverLay
{
    CLLocationCoordinate2D coordinateArray[[self.workout.route count]];

    for (int i = 0; i < [self.workout.route count]; i++) {
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = [[[self.workout.route objectAtIndex:i] latitude] doubleValue];
        coordinate.longitude = [[[self.workout.route objectAtIndex:i] longitude] doubleValue];
        coordinateArray[i] = coordinate;
    }

    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:[self.workout.route count]];
    [self.mapView addOverlay:self.routeLine];
    [self setVisibleMapRect];
}

使用标量可以提高性能吗?或者我应该在保存它们时尝试用某种算法过滤掉其中一些位置点?

4

2 回答 2

0

以下是一些优化建议:

首先,您count+1调用了count(>2000)。将计数存储在变量中。

其次,在您的循环中,您反复从锻炼对象中检索数据。route在开始循环之前尝试存储数组。

此外,如果是从toroute的一对多关系,它应该导致一个,而不是一个数组。我怀疑您正在使用,这也可能影响您的表现。使用简单的整数属性跟踪订单可能会更好。WorkoutLocationNSSetNSOrderdSet

于 2013-01-15T11:30:09.873 回答
0

这里的技巧是在数据库端进行排序。

- (void)createRouteLineAndAddOverLay
{
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Location"];
    NSSortDescriptor *fromStartToEnd = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
    request.sortDescriptors = [NSArray arrayWithObject:fromStartToEnd];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"workout = %@", self.workout];
    request.predicate = predicate;
    NSArray *locations = [self.workout.managedObjectContext executeFetchRequest:request error:NULL];

    int routeSize = [locations count];

    CLLocationCoordinate2D coordinateArray[routeSize];

    for (int i = 0; i < routeSize; i++) {
        CLLocationCoordinate2D coordinate;
        coordinate.latitude = [[[locations objectAtIndex:i] latitude] doubleValue];
        coordinate.longitude = [[[locations objectAtIndex:i] longitude] doubleValue];
        coordinateArray[i] = coordinate;
    }

    self.routeLine = [MKPolyline polylineWithCoordinates:coordinateArray count:routeSize];
    [self.mapView addOverlay:self.routeLine];
    [self setVisibleMapRect];
}

当锻炼有 1321 个位置时,这种方法只用了 0.275954 秒

于 2013-01-20T19:44:33.173 回答