LiveData.kt 646 Bytes
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.fenix.ext

import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer

/**
 * Observe a LiveData once and unregister from it as soon as the live data returns a value
 */
13
fun <T> LiveData<T>.observeOnceAndRemoveObserver(callback: (T) -> Unit) {
14
15
16
    observeForever(object : Observer<T> {
        override fun onChanged(value: T) {
            removeObserver(this)
17
            callback(value)
18
19
20
        }
    })
}