问题
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