# Handle LiveData and Observable to return inside a function a value emmited

**URL:** <https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180>\
**Category:** Android\
**Created:** [September 15, 2020, 11:34pm UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180 "2020-09-15T23:34:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexandre9865](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexandre9865](https://discuss.kotlinlang.org/u/alexandre9865)\
**Post date:** [September 15, 2020, 11:34pm UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180/1 "2020-09-15T23:34:06Z")

</div>

I’m starting in MVVM architecture and Kotlin reactive programming.  
How can I use kotlin `LiveData, Coroutines and/or Observable` to do in `optionSelected()` function a return only after the user click in par1 or par2 buttons to return the content to second while of `insertionSort()` function?

```
class PlaceholderFragment : Fragment() {
    var par1:Button? = null
    var par2:Button? = null
    var parTextLive: MutableLiveData<String> = MutableLiveData<String>()

    var arr = arrayOf("homework", "chores", "shopping")
    var i = -1
    var j: Int = 0
    var tmp: String = ""
    var clicked: Boolean = false
    var parText: String = ""
    var len: Int = arr.size

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_main, container, false)
        par1 = root!!.findViewById(R.id.par1) as Button
        par2 = root!!.findViewById(R.id.par2) as Button

        lifecycleScope.launch {
            insertionSort()
        }
        par1!!.setOnClickListener {
            lifecycleScope.launch {

                parTextLive.value = (par1!!.text.toString())
                parText = (par1!!.text.toString())
            }
        }
        par2!!.setOnClickListener {
            lifecycleScope.launch {

                parTextLive.value = (par2!!.text.toString())
                parText = (par2!!.text.toString())
            }
        }
        return root
    }

    suspend fun optionSelected(str1: String, str2: String): String {
        return withContext(Dispatchers.Main) {
            println("opções setadas: $str1 or $str2")
            par1!!.text = str1
            par2!!.text = str2

            // I setted the buttons pair1 and pair2 with texts of str1 and str2. When the user click in one of them, I want to return the text of clicked button 

            delay(5000) //substitute the delay by some emitter event of user click

            return@withContext str1 //returning the variable parTextLive with the refreshed value
        }
    }

    suspend fun insertionSort(): Array<String> {
        return withContext(Dispatchers.Default) {
            while(len-- != 0) {
                tmp = arr[++i];
                j = i
                while (j-- != 0 && (optionSelected(arr[j], tmp) == arr[j])) {
                    arr[j + 1] = arr[j];
                }
                arr[j + 1] = tmp
            }
            return@withContext arr.apply { reverse() }
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [September 16, 2020, 5:02am UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180/2 "2020-09-16T05:02:19Z")

</div>

Perhaps you are looking for something like:

```auto
parTextLive.asFlow().first()

```

This will wait for `parTextLive` to have a value.

---

<div class="post-metadata">

**Author:** ![alexandre9865](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexandre9865](https://discuss.kotlinlang.org/u/alexandre9865)\
**Post date:** [September 16, 2020, 12:01pm UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180/3 "2020-09-16T12:01:35Z")

</div>

> [@nickallendev](#):
>
> `asFlow().first()`

@nickallendev I’ve imported kotlinx.coroutines.flow.asFlow in file, but the **asFlow** function appears as _ **Unresolved reference** _

I have to import another library?

---

<div class="post-metadata">

**Author:** ![nickallendev](https://avatars.discourse-cdn.com/v4/letter/n/cdc98d/32.png) [@nickallendev](https://discuss.kotlinlang.org/u/nickallendev)\
**Post date:** [September 16, 2020, 3:52pm UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180/4 "2020-09-16T15:52:56Z")

</div>

The extension method is in package `androidx.lifecycle` in library “androidx.lifecycle:lifecycle-livedata-ktx:2.2.0”

You can check out [List of KTX extensions](https://developer.android.com/kotlin/ktx/extensions-list#androidxlifecycle) when having trouble finding where extension methods are defined.

---

<div class="post-metadata">

**Author:** ![alexandre9865](https://avatars.discourse-cdn.com/v4/letter/a/258eb7/32.png) [@alexandre9865](https://discuss.kotlinlang.org/u/alexandre9865)\
**Post date:** [September 16, 2020, 5:29pm UTC](https://discuss.kotlinlang.org/t/handle-livedata-and-observable-to-return-inside-a-function-a-value-emmited/19180/5 "2020-09-16T17:29:37Z")

</div>

It works fine now, many thanks @nickallendev
