Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

173
Vistas
Make sure value returned from coroutine overwrites default value in android kotlin

I would like to make sure queried value from database comes always later than default value.

For example

class SetTimeFragment : Fragment(){

    private lateinit var binding: FragmentSetTimeBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentSetTimeBinding.inflate(layoutInflater, container, false)

        val dbId = 3
        val defaultTime = LocalTime.now().toString()

        //Set default value
        binding.timeView.setText(defaultTime)


        lifecycleScope.launchWhenCreated {

            val itemList = withContext(Dispatchers.IO) {

                //This is Database query
                ObjectBox.store.boxFor(Data::class.java)[dbId]
            }

            // string from database e.g.12:30
            val newTime = itemList.dbTimeStr

            //overwrite default value with database query result
            binding.timeView.setText(newTime)
        }


        return binding.root
    }

} 

Currently, this code has no problem. But does coroutine possibly return value faster than default value? I mean, database value goes to view first, and default value overwrites it. If it possibly happens, how can I prevent it? Thank you.

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

The coroutine doing the DB query is not even started when the default value is set on the text view, so there is no risk it can finish before that and write a value that would be overwritten by the default

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda