我在使用Google API终止应用程序时遇到了此错误,这是由于未被发现的例外GMSThreadException
-(void)drawRoute
{
dispatch_queue_t myQueue = dispatch_queue_create("My Queue",NULL);
dispatch_async(myQueue, ^{
[self fetchPolylineWithOrigin:origin destination:destination completionHandler:^(GMSPolyline *polyline)
{
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
if(polyline)
polyline.map = mapView;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
});
}];
});
}
也要在主线程上使用GMSpath和GMSpolyline,然后它应该起作用。
从您的错误消息中,您似乎只能从主线程调用该API方法,尝试使用:
-(void)drawRoute
{
dispatch_async(dispatch_get_main_queue(), ^{
[self fetchPolylineWithOrigin:origin destination:destination completionHandler:^(GMSPolyline *polyline)
{
// Update the UI
if(polyline)
polyline.map = mapView;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
});
}
这仍然允许您的代码在消失并执行此操作时继续进行,因为它是异步的,但将其保留在主线程(队列)上。该方法具有完整处理程序的事实表明它本身是异步的,也许您根本不需要在这里进行派遣?
我的赌注是您可以做:
-(void)drawRoute
{
[self fetchPolylineWithOrigin:origin destination:destination completionHandler:^(GMSPolyline *polyline)
{
// Update the UI
if(polyline)
polyline.map = mapView;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}];
}
或您以前是否遇到过这种性能问题?
在我的情况下,要求在两个地方使用主线程
一个是...
__block GMSMapView *mapView;
dispatch_async(dispatch_get_main_queue(), ^{
// Create GMSMapView
mapView = [GMSMapView mapWithFrame:CGRectMake(1, 1, _subView.frame.size.width-2, _subView.frame.size.height-2) camera:camera];
mapView.myLocationEnabled = YES;
[_subView addSubview:mapView];
});
第二个是...
- (void)fetchPolylineWithOrigin:(CLLocation *)origin destination:(CLLocation *)destination completionHandler:(void (^)(GMSPolyline *))completionHandler {
// Code here ....
__block GMSPolyline *polyline = nil;
dispatch_async(dispatch_get_main_queue(), ^{ // Second main
if ([routesArray count] > 0) {
NSDictionary *routeDict = [routesArray objectAtIndex:0];
NSDictionary *routeOverviewPolyline = [routeDict objectForKey:@"overview_polyline"];
NSString *points = [routeOverviewPolyline objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:points];
polyline = [GMSPolyline polylineWithPath:path];
}
});
}