Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!