Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  2. ...
  3. GlobalScope.launch(Dispatchers.Main) {
  4. val job = GlobalScope.launch(Dispatchers.IO) {
  5. val task = async(Dispatchers.IO) {
  6. settingsInteractor.getStationSearchCountry().let {
  7. countryName = it.name
  8. }
  9. settingsInteractor.getStationSearchRegion().let {
  10. regionName = it.name
  11. }
  12. }
  13. task.await()
  14. }
  15. job.join()
  16. }
  17. }
  18.  
  19. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  20. super.onViewCreated(view, savedInstanceState)
  21.  
  22. country.updateCaption(countryName)
  23. region.updateCaption(regionName)
  24. }
  25.  
  26. private var job: Job = Job()
  27. override val coroutineContext: CoroutineContext
  28. get() = Dispatchers.Main + job
  29.  
  30. @Override
  31. public void onDestroy() {
  32. super.onDestroy();
  33. job.cancel()
  34. }
  35.  
  36. implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'
  37.  
  38. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  39. super.onViewCreated(view, savedInstanceState)
  40. launch {
  41. val operation = async(Dispatchers.IO) {
  42. settingsInteractor.getStationSearchCountry().let {
  43. countryName = it.name
  44. }
  45. settingsInteractor.getStationSearchRegion().let {
  46. regionName = it.name
  47. }
  48. }
  49. operation.await() // wait for result of I/O operation without blocking the main thread
  50.  
  51. // update views
  52. country.updateCaption(countryName)
  53. region.updateCaption(regionName)
  54. }
  55. }
  56.  
  57. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  58. super.onViewCreated(view, savedInstanceState)
  59.  
  60. GlobalScope.launch(Dispatchers.IO) {
  61. settingsInteractor.getStationSearchCountry().let {
  62. countryName = it.name
  63. }
  64. settingsInteractor.getStationSearchRegion().let {
  65. regionName = it.name
  66. }
  67.  
  68. launch(Dispatchers.Main) {
  69. country.updateCaption(countryName)
  70. region.updateCaption(regionName)
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement