Skip to content

Commit

Permalink
fix: sync issues and don't use main thread for DB access
Browse files Browse the repository at this point in the history
  • Loading branch information
Yash-Garg committed Sep 8, 2023
1 parent 43533d2 commit 4a82eee
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ constructor(
}

private suspend fun checkIfConfigsExist() {
configDao.getConfigs().collect { configs ->
if (configs.isNotEmpty()) {
_configStatus.emit(ConfigStatus.EXISTS)
checkAndGetClient()
} else {
_configStatus.emit(ConfigStatus.DOES_NOT_EXIST)
withContext(Dispatchers.IO) {
configDao.getConfigs().collect { configs ->
if (configs.isNotEmpty()) {
_configStatus.emit(ConfigStatus.EXISTS)
checkAndGetClient()
} else {
_configStatus.emit(ConfigStatus.DOES_NOT_EXIST)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import qbittorrent.QBittorrentClient

@HiltViewModel
Expand Down Expand Up @@ -187,7 +188,7 @@ class ConfigViewModel @Inject constructor(private val configDao: ConfigDao) : Vi
trustSelfSigned = trustSelfSigned
)

viewModelScope.launch { configDao.addConfig(config) }
viewModelScope.launch { withContext(Dispatchers.IO) { configDao.addConfig(config) } }
}

suspend fun testConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ServerViewModel @Inject constructor(private val repository: QbitRepository
private var syncJob: Job? = null

init {
refresh()
syncJob = viewModelScope.launch { syncData() }
}

fun refresh() {
Expand Down

0 comments on commit 4a82eee

Please sign in to comment.