私は、ポイント(MKAnnotationView)をマップ(MKMapView)にロードするiOSアプリケーションを使用していますが、ポイントをオンまたはオフにすることができます)。Objective-Cのコードを最適化して配列を検索する
私はJSONを使用してデータベースからポイントを引き出し、3つの配列にオブジェクトを読み込みます:locsは位置(名前、ID、座標、説明、画像データ)の配列です。catsはカテゴリ(name 、ID、画像データ)であり、タグはカテゴリ - ロケーションのペアの集合(カテゴリID &ロケーションID)である。
マップにポイントを追加するには、場所がどのカテゴリにあるのかを特定するループを2つ使用して、そのカテゴリがマップ上にあるかどうかを確認します。問題は、場所を追加するこの方法がデバイスで実行されているときに長時間かかることです(iPhone 4Sでは約6〜10秒)。私はすべてを高速化するこのコードを最適化する良い方法があるのだろうかと思っています。
ここに私のコードがあります。このコードは、mapviewがロードされるたびに実行されます。ポイント地図上に表示することができ、すべての点、shownPointsの配列であることは、そのカテゴリを有効にしたすべてのポイントの配列です:
[self.mapView removeAnnotations:points];
for (searchLocation *tempLoc in locs)
{
name = tempLoc.name;
description = tempLoc.description;
latiString = tempLoc.latiString;
longiString = tempLoc.longiString;
coordinate.latitude = latiString.doubleValue;
coordinate.longitude = longiString.doubleValue;
imageData = tempLoc.picture;
MapViewAnnotation *destinationPoint = [[MapViewAnnotation alloc] initWithTitle:name andCoordinate:coordinate andDescription:description andImageData:imageData];
[points addObject:destinationPoint];
for (CatTag *tempCatTag in tags)
{
if ([tempCatTag.locationID isEqualToString: tempLoc.locID])
{
for (Category *tempCat in cats)
{
if ([tempCatTag.categoryID isEqualToString:tempCat.catID] &&
[[shownCategories objectAtIndex:[cats indexOfObject:tempCat]] isEqualToString: @"YES"])
{
[shownPoints addObject: destinationPoint];
}
}
}
}
}
[self.mapView addAnnotations:shownPoints];
}
どのようなタイプの 'locationID' /' locID'と 'categoryID' /' catID'ですか? – Costique
'[[NSString stringWithFormat:@"%@ "、tempLoc.name] init]'は非常に間違っています。ちょうど 'tempLoc.name'を使用してください。 '[[NSString alloc] initWithFormat:@"%@ "、tempCat.catID]'と同じです。 – jtbandes
'locationID/locID'と' categoryID/catID'の両方がNSStringです。値はすべて数値なので、intに変換すれば問題ありません。 –