1

私のアプリは MapKit ベースで、複数のユーザーを追跡できます。現在、私たちの Web サービスを使用して、地図上に自分の場所と他のユーザーの最後の場所 (10 か所としましょう) を表示しています。ユーザーが自分の位置を更新すると、Web サービスを介して送信され、コールバックを介してマップに表示されます。他のユーザーをリアルタイムで追跡できますが、ここでスレッディングを使用する方法がわかりません。メモリの問題が原因で、UI がブロックされたり、クラッシュしたりすることがあります。

私のconnectionDidFinishLoading方法では、JSON データを解析してから、注釈とオーバーレイを作成しています。

-(void) connectionDidFinishLoading: (NSURLConnection *) connection
{

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;

NSArray *trackingDict = [NSJSONSerialization JSONObjectWithData:empJsonData options:kNilOptions error:nil];
NSLog(@"Json Dictionary = %@", trackingDict);
NSLog(@"COUNT = %i",trackingDict.count);

if ([trackingDict count] >= 2) {
    for (trackUsersCount = 0; trackUsersCount< trackingDict.count; trackUsersCount++) {
        NSLog(@"trackUsersCount %i", trackUsersCount);

        NSMutableArray *latlongArray = [[NSMutableArray alloc]init];
        latlongArray = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"latlong"];

        [userLongitudeArray removeAllObjects];
        [userLatitudeArray removeAllObjects];

        for (int i = 0; i<latlongArray.count; i++) {
            NSLog(@"COunt - > %@", [[latlongArray objectAtIndex:i]objectForKey:@"lat"]);
            NSLog(@"COunt - > %@", [[latlongArray objectAtIndex:i]objectForKey:@"long"]);
            [userLatitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"lat"]];
            [userLongitudeArray addObject:[[latlongArray objectAtIndex:i]objectForKey:@"long"]];

        }

        // ProfilePIC URL
        profilePicURLString = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_profilePicture"];

NSString *name = [[trackingDict objectAtIndex:trackUsersCount]objectForKey:@"user_firstName"];
        [userNameArray addObject:name];
        [profilePicURLStringArray addObject:profilePicURLString];

        for (int i = 0; i<userLatitudeArray.count; i++) {
            CLLocationCoordinate2D userLocation;
            userLocation.latitude = [[userLatitudeArray objectAtIndex:i]doubleValue];
            userLocation.longitude = [[userLongitudeArray objectAtIndex:i] doubleValue];
            Annotation *Anno = [[Annotation alloc]init];

            Anno.coordinate = userLocation;
            Anno.title = name;
            Anno.userProfileImageString = profilePicURLString;
            [mapView addAnnotation:Anno];
        }

        NSLog(@"ARRAY for longitude %@", userLongitudeArray);
        NSLog(@"ARRAY for latitude %@", userLatitudeArray);
        int i;
        for (i = 0; i<userLatitudeArray.count; i++) {
            CLLocationCoordinate2D userLocation;
            userLocation.latitude = [[userLatitudeArray objectAtIndex:i]doubleValue];
            userLocation.longitude = [[userLongitudeArray objectAtIndex:i] doubleValue];
            MKMapPoint * pointsArray = malloc(sizeof(CLLocationCoordinate2D)*userLongitudeArray.count);
            pointsArray[i] = MKMapPointForCoordinate(userLocation);
            polyline = [MKPolyline polylineWithPoints:pointsArray count:i];
            free(pointsArray);
        }
         [mapView addOverlay:polyline];
         }
}

[mapView reloadInputViews];

}

}

Web サービスは 20 秒ごとに呼び出されます。ここで GCD または他のスレッド アプローチを使用できることはわかっていますが、バックグラウンド スレッドを介して Web サービスが呼び出される時間間隔では、注釈とオーバーレイはマップではなく表示されません。

どんな助けでも大歓迎です!

4

1 に答える 1

0

これに対する迅速で簡単な修正はないと思います。1 つのアプローチは、ビューを更新するコードからデータを収集 (変更) するコードを分離することです。

たとえば、UI を扱っているコードを別のメソッドに設定しupdateUIます。

ここから、いくつかの選択肢があります。たとえば、これを試すことができます:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
   //perform the data collection, calculations... 
   //here is where the model in MVC gets modified
   //
   [self performSelectorOnMainThread: @selector(updateUI) withObject:nil waitUntilDone: NO];
}

-(void)updateUI
{
   //do the UI updates (like adding overlays etc...) here 
}

UI を一種のオブジェクトに更新するために必要なすべてのデータを格納し、それをwithObject:パラメーターとして渡すこともできます。

于 2013-01-29T13:09:43.520 に答える