Tony Kazanjian
02/22/2022, 2:05 AMNativeViewModel
and have my iOS network implementation done separately in an interface using URLSession
. Is there anything within the Kotlin/Native concurrency model that makes calling a URLSession
with a completion handler callback impossible? I'm pretty sure I've done everything I can to make my data state immutable.
class SearchViewModel(
private val repository: SearchRepository,
private val onSearchState: (SearchState) -> Unit
): KoinComponent {
private val scope = MainScope(Dispatchers.Main)
private val _searchStateFlow = MutableStateFlow(SearchState())
init {
ensureNeverFrozen()
observeQueries(_searchStateFlow.value.query)
}
@OptIn(FlowPreview::class)
@Throws(Exception::class)
fun observeQueries(query: String) {
_searchStateFlow.value = SearchState(
query = query,
isLoading = true)
scope.launch {
repository.searchGames(_searchStateFlow.asStateFlow())
.collect { state ->
onSearchState(state)
}
}
}
}
The error happens on the completion handler that is called in the IOSSearchInteractor
's URLSession callback:
class IOSSearchInteractor: SearchInteractor {
func searchGames(query: String, completion: @escaping (Result<String, Error>) -> Void) {
var request ...
let task = URLSession.shared.dataTask(with: request) { data, response, error in
// error happens here on completion handler
let str = String(decoding: data!, as: UTF8.self)
completion(.success(str))
}
task.resume()
}
russhwolf
02/22/2022, 1:43 PMcompletion
call in DispatchQueue.main.async { }
.
Alternatively, consider trying out the new memory model.kpgalligan
02/22/2022, 2:52 PMTony Kazanjian
02/23/2022, 8:01 AM