iOS中的多线程,MapKit显示数据

iOS中的多线程,MapKit显示数据,ios,multithreading,ipad,mapkit,mkoverlay,Ios,Multithreading,Ipad,Mapkit,Mkoverlay,我的应用程序基于MapKit,可以跟踪多个用户。现在使用我们的web服务,我在地图上显示我的位置以及其他用户的最后位置,比如说10个位置。如果用户更新了他们的位置,它将通过web服务发送,并通过回调显示在地图上。我能够实时跟踪其他用户,但不知道如何在这里使用线程。我的UI有时会阻塞,有时也会因为内存问题而崩溃 在我的connectiondFinishLoading方法中,我解析JSON数据,然后创建注释和覆盖: -(void) connectionDidFinishLoading: (NSURL

我的应用程序基于MapKit,可以跟踪多个用户。现在使用我们的web服务,我在地图上显示我的位置以及其他用户的最后位置,比如说10个位置。如果用户更新了他们的位置,它将通过web服务发送,并通过回调显示在地图上。我能够实时跟踪其他用户,但不知道如何在这里使用线程。我的UI有时会阻塞,有时也会因为内存问题而崩溃

在我的
connectiondFinishLoading
方法中,我解析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];

}
-(void)连接dFinishLoading:(NSURLConnection*)连接
{
[UIApplication sharedApplication].networkActivityIndicatorVisible=否;
NSArray*trackingDict=[NSJSONSerialization JSONObjectWithData:empJsonData选项:编织错误:nil];
NSLog(@“Json字典=%@”,trackingDict);
NSLog(@“COUNT=%i”,trackingDict.COUNT);
如果([trackingDict count]>=2){
对于(TrackUserScont=0;TrackUserScont对于(int i=0;i我认为没有一个快速简单的解决方法。一种方法是将收集(修改)数据的代码与更新视图的代码分开

将处理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:
参数传递。

可以使用后台线程修改数据(模型)但是你真的不应该从后台线程修改UI。谢谢@rokjarc的回复。我真的需要知道在我的情况下我将如何处理线程。任何示例片段都会非常有用。好的,谢谢,我会尝试,只问一个与你上一个答案相关的问题,如果我用单独的方法处理UI(就像你写的那样)
connectiondFinishLoading
方法不会阻止我的UI吗?因为数据仍然是通过这种方法到达的。刚刚找到了一个很好的答案,可以完全帮助你:它处理图像下载,但基本上是一样的。