0

Core Location フレームワークを使用してユーザーの動きを追跡するフィットネス アプリケーションを構築しています。Core Data フレームワークを使用してデータを保存しています。現在、私は 2 つのエンティティを持っています。ワークアウトと場所。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

ここでのコツは、データベース側でソートを行うことです。

- (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 に答える
0

最適化のためのいくつかの提案を次に示します。

まず、(>2000)count+1への呼び出しがあります。countカウントを変数に格納します。

次に、ループ内でワークアウト オブジェクトからデータを繰り返し取得しています。routeループを開始する前に配列を保存してみてください。

また、 が to から への対多関係である場合、route結果は配列ではなく になります。を使用していると思われますが、これもパフォーマンスに影響する可能性があります。単純な整数属性を使用して順序を追跡する方がよいでしょう。WorkoutLocationNSSetNSOrderdSet

于 2013-01-15T11:30:09.873 に答える