From 707b59cc6a22c39528e3350b70c94a5b2ccb30d4 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 22 Dec 2019 22:13:05 +0200 Subject: [PATCH] Hopefully solved ANRs when first entering a large multiplayer game --- .../optionstable/WorldScreenMenuTable.kt | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenMenuTable.kt b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenMenuTable.kt index 8766156a945fc..27790031d7dc0 100644 --- a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenMenuTable.kt +++ b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenMenuTable.kt @@ -17,6 +17,7 @@ import com.unciv.ui.utils.toLabel import com.unciv.ui.worldscreen.WorldScreen import java.util.* import kotlin.collections.ArrayList +import kotlin.concurrent.thread class WorldScreenMenuTable(val worldScreen: WorldScreen) : PopupTable(worldScreen) { @@ -117,13 +118,18 @@ class WorldScreenMenuTable(val worldScreen: WorldScreen) : PopupTable(worldScree badGameIdLabel.isVisible = true return@addButton } - try { - val game = OnlineMultiplayer().tryDownloadGame(gameId.trim()) - UncivGame.Current.loadGame(game) - } catch (ex: Exception) { - badGameIdLabel.setText("Could not download game!".tr()) - badGameIdLabel.isVisible = true - return@addButton + thread { + try { + // The tryDownload can take more than 500ms. Therefore, to avoid ANRs, + // we need to run it in a different thread. + val game = OnlineMultiplayer().tryDownloadGame(gameId.trim()) + // The loadGame creates a screen, so it's a UI action, + // therefore it needs to run on the main thread so it has a GL context + Gdx.app.postRunnable { UncivGame.Current.loadGame(game) } + } catch (ex: Exception) { + badGameIdLabel.setText("Could not download game!".tr()) + badGameIdLabel.isVisible = true + } } }.row() multiplayerPopup.add(badGameIdLabel).row()