Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Kotlin协同程序中的挂起函数问题_Android_Kotlin_Android Room_Kotlin Coroutines - Fatal编程技术网

Android Kotlin协同程序中的挂起函数问题

Android Kotlin协同程序中的挂起函数问题,android,kotlin,android-room,kotlin-coroutines,Android,Kotlin,Android Room,Kotlin Coroutines,我在下面的视频中使用Kotlin协同程序: 视频中的9:30。 但当我编写类似的代码时,它的行为与我预期的不同: WalletDao.kt interface WalletDao { @Query("SELECT * FROM wallet LIMIT 1") suspend fun getFirstWallet(): Wallet } class Repository private constructor(appContext: Context) {

我在下面的视频中使用Kotlin协同程序: 视频中的9:30。 但当我编写类似的代码时,它的行为与我预期的不同:

WalletDao.kt

interface WalletDao {
    @Query("SELECT * FROM wallet LIMIT 1")
    suspend fun getFirstWallet(): Wallet
}
class Repository private constructor(appContext: Context) {
    private val appDatabase = AppDatabase.getInstance(appContext)
//current wallet
    private var _currentWallet : MutableLiveData<Wallet> = MutableLiveData()
    var currentWallet : LiveData<Wallet> = _currentWallet
    suspend fun initFirstWallet()
    {
        val fW = withContext(Dispatchers.IO)
        {
            appDatabase.walletDao.getFirstWallet()
        }
        _currentWallet.value = fW
    }
}
class Repository private constructor(appContext: Context) {
    suspend fun initFirstWallet()
    {
        withContext(Dispatchers.IO)
        {
            _currentWallet.postValue(appDatabase.walletDao.getFirstWallet())
        }
    }
}
Repository.kt

interface WalletDao {
    @Query("SELECT * FROM wallet LIMIT 1")
    suspend fun getFirstWallet(): Wallet
}
class Repository private constructor(appContext: Context) {
    private val appDatabase = AppDatabase.getInstance(appContext)
//current wallet
    private var _currentWallet : MutableLiveData<Wallet> = MutableLiveData()
    var currentWallet : LiveData<Wallet> = _currentWallet
    suspend fun initFirstWallet()
    {
        val fW = withContext(Dispatchers.IO)
        {
            appDatabase.walletDao.getFirstWallet()
        }
        _currentWallet.value = fW
    }
}
class Repository private constructor(appContext: Context) {
    suspend fun initFirstWallet()
    {
        withContext(Dispatchers.IO)
        {
            _currentWallet.postValue(appDatabase.walletDao.getFirstWallet())
        }
    }
}
当我在MainActivity中运行代码时,有时会调用viewModel.currentWallet的lambda,并显示当前钱包的名称,但大部分时间不会调用它。 我已将代码更改为以下内容,但没有任何更改:

Repository.kt

interface WalletDao {
    @Query("SELECT * FROM wallet LIMIT 1")
    suspend fun getFirstWallet(): Wallet
}
class Repository private constructor(appContext: Context) {
    private val appDatabase = AppDatabase.getInstance(appContext)
//current wallet
    private var _currentWallet : MutableLiveData<Wallet> = MutableLiveData()
    var currentWallet : LiveData<Wallet> = _currentWallet
    suspend fun initFirstWallet()
    {
        val fW = withContext(Dispatchers.IO)
        {
            appDatabase.walletDao.getFirstWallet()
        }
        _currentWallet.value = fW
    }
}
class Repository private constructor(appContext: Context) {
    suspend fun initFirstWallet()
    {
        withContext(Dispatchers.IO)
        {
            _currentWallet.postValue(appDatabase.walletDao.getFirstWallet())
        }
    }
}

你能告诉我上面的代码有什么问题吗???多谢各位

您不需要为此使用协程

WalletDao.kt

interface WalletDao {
    @Query("SELECT * FROM wallet LIMIT 1")
    fun getFirstWallet(): LiveData<Wallet>
}
MainActivityViewModel.kt

class MainActivityViewModel(private val repository: Repository) : ViewModel()
{
    var currentWallet: LiveData<Wallet> = repository.currentWallet
    init {
        viewModelScope.launch {
            repository.initFirstWallet()
        }
    }
}
class MainActivityViewModel(private val repository: Repository) : ViewModel()
{
    var currentWallet: LiveData<Wallet> = repository.initFirstWallet()
    
}
class MainActivityViewModel(私有val存储库:存储库):ViewModel()
{
var currentWallet:LiveData=repository.initFirstWallet()
}

这将正常工作,不会出现任何线程问题。

您不必在存储库中使用
withContext(Dispatchers.IO)
,我认为
房间
将为您处理。删除并仅使用
\u currentWallet.postValue(appDatabase.walletDao.getFirstWallet())进行尝试。