MKMapView Not Loading When Called on an NSThread?

一个人想着一个人 提交于 2019-12-13 03:04:03

问题


I am creating a MKMapView in a method named "generateMap". From inside viewDidLoad, this works:

[self generateMap];

but this causes the map to quickly load and then disappear, leaving only the blank grey grid:

[NSThread detachNewThreadSelector:@selector(generateMap) toTarget:self withObject:nil];

Any ideas why this might be happening when I call the method through a thread?


I ended up doing this:

-(void)viewDidLoad {
[NSThread detachNewThreadSelector:@selector(spinTheSpinner) toTarget:self withObject:nil];
[self performSelectorOnMainThread:@selector(generateMap) withObject:nil waitUntilDone:NO];
[super viewDidLoad];
}

This allows me to have a spinner (UIActivityIndicator) and load the MKMapView as I want. I set "waitUntilDone:No]" so that the screen switches the MapView before it is done with generateMap. Otherwise, we would not see the spinner and would only see a blank screen until generateMap was done.


回答1:


I don't think it is advisable to try to update the UI through anything but the main thread.

What happens when you try to run -generateMap on the main thread with -performSelectorOnMainThread:withObject:waitUntilDone:?

For example:

[self performSelectorOnMainThread:@selector(generateMap) withObject:nil waitUntilDone:YES];



回答2:


First off, can I see the generateMap method? Second, a more sufficient way to perform a selector is by saying


[self performSelector:@selector(generateMap)];



来源:https://stackoverflow.com/questions/2648538/mkmapview-not-loading-when-called-on-an-nsthread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!