Closed lewisjwilson closed 3 years ago
Attempted a fix by changing debounce time from 300ms to 500ms
debounce is not the issue.
Not understanding flow correctly.
Get API data via flow emit (in collect statement), then add data to recyclerview. Right now, data is not emitting. recyclerview updates within the flow (launch).
import kotlinx.coroutines.*
import kotlin.coroutines.*
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.*
import kotlin.coroutines.CoroutineContext
val namesFlow = flow {
val names = listOf("Jody", "Steve", "Lance", "Joe")
for (name in names) {
delay(100)
emit(name)
}
}
fun main() = runBlocking {
namesFlow
.mapLatest {
if(it.length>3){
println(it)
}
}
.catch { println("Error!") }
.collect { it }
println()
}
Check to see how flow functions: Online Compiler
Abort using flow. Update SearchView code as follows?
@Override
public boolean onQueryTextChange(String searchTerm) {
mQueryString = searchTerm;
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
//Put your call to the server here (with mQueryString)
}
}, 300);
return true;
}
OR
var queryTextChangedJob: Job? = null
...
fun onQueryTextChange(query: String) {
queryTextChangedJob?.cancel()
queryTextChangedJob = launch(Dispatchers.Main) {
delay(500)
performSearch(query)
}
}
Removed flow and replaces with a coroutine with a delay of 1000ms (open to change)
Not solved. Delays search, however consider the following:
This means the search terms are combined in mSearchList, displaying results from both "ha" and "hat".
Somehow in the coroutine, must clear mSearchList, however 2 instances of dataFromNetwork are running. Possible solution:
Create a check in dataFromNetwork to see if queryjob is null (set null on new text change). Inside for loop (getting new data for recycler:
if (queryjob = null){
break
}
Also see job.isActive()
Pretty much working. Functional, just a little slow.
Search updates at the incorrect time. Fix this in "searchpage.kt"