Skip to content

Commit

Permalink
Hopefully solved ANRs when first entering a large multiplayer game
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Dec 22, 2019
1 parent 3ef34f9 commit 707b59c
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 707b59c

Please sign in to comment.