私のアプリは 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 サービスが呼び出される時間間隔では、注釈とオーバーレイはマップではなく表示されません。
どんな助けでも大歓迎です!