views:

115

answers:

2

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.

A: 

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


[self performSelector:@selector(generateMap)];

Flafla2
Or, simply, `[self generateMap];`
Alex Reynolds
well, both work, but I find my method to have less errors.
Flafla2
A: 

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];
Alex Reynolds
I set ...waitUntilDone:NO]; so the screen does not sit there blank.
Chris