在 NSThread 上调用时 MKMapView 未加载?

发布于 2024-08-29 05:41:47 字数 726 浏览 2 评论 0原文

我正在名为“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];
}

这允许我有一个微调器(UIActivityIndi​​cator)并根据需要加载 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任性一次 2024-09-05 05:41:47

我认为不建议尝试通过主线程以外的任何方式更新 UI。

当您尝试使用 -performSelectorOnMainThread:withObject:waitUntilDone: 在主线程上运行 -generateMap 时会发生什么?

例如:

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

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];
-柠檬树下少年和吉他 2024-09-05 05:41:47

首先,我可以看到 generateMap 方法吗?其次,执行选择器的更充分的方法是说


[self performSelector:@selector(generateMap)];

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


[self performSelector:@selector(generateMap)];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文