在 NSThread 上调用时 MKMapView 未加载?
我正在名为“generateMap”的方法中创建 MKMapView。从 viewDidLoad 内部,这是可行的:
[self generateMap];
但这会导致地图快速加载然后消失,只留下空白的灰色网格:
[NSThread detachNewThreadSelector:@selector(generateMap) toTarget:self withObject:nil];
有什么想法为什么当我通过线程调用该方法时可能会发生这种情况?
我最终这样做了:
-(void)viewDidLoad {
[NSThread detachNewThreadSelector:@selector(spinTheSpinner) toTarget:self withObject:nil];
[self performSelectorOnMainThread:@selector(generateMap) withObject:nil waitUntilDone:NO];
[super viewDidLoad];
}
这允许我有一个微调器(UIActivityIndicator)并根据需要加载 MKMapView。我设置了“waitUntilDone:No]”,以便屏幕在使用generateMap完成之前切换MapView。否则,我们将看不到微调器,并且只会看到空白屏幕,直到生成映射完成为止。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为不建议尝试通过主线程以外的任何方式更新 UI。
当您尝试使用
-performSelectorOnMainThread:withObject:waitUntilDone:
在主线程上运行-generateMap
时会发生什么?例如:
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:
首先,我可以看到
generateMap
方法吗?其次,执行选择器的更充分的方法是说First off, can I see the
generateMap
method? Second, a more sufficient way to perform a selector is by saying