julioromano
07/02/2021, 2:18 PMBasicTextFiled
?
More details in đź§µjulioromano
07/02/2021, 2:20 PMvar myString by mutableStateOf("")
BasicTextField(
value = myString,
onValueChange = {
myString = it
}
)
Everything works fine here. The myString
property is hoisted very close to the text field and the resulting two way data binding works very fast. Typing quickly on a keyboard shows the reflected input displayed on the screen with no hiccups.
Example 2:
val myString: String by myBusinessLogic.queryFlow.collectAsStateWithLifecycle(initial = "")
BasicTextField(
value = myString,
onValueChange = {
myBusinessLogic.persistMyString(it) // async call, this returns immediately.
}
)
Here myString
has been hoisted into some business logic component.
Let’s imagine, for instance, that in this component we persist the onValueChange
string in a SQLite DB and that we then query for such a string from the DB exposing it as a Flow
which in the end updates the value
string shown in the text field.
This is where interactivity problems start: The round trip time of this two way data binding is of course greater because there’s a DB in between. Typing quickly on the keyboard or using keyboard auto repeat for long time starts showing some hiccups: sometimes you end up with some chars after where the cursor is.
Intuitively I understand that I’m doing something “wrong” here, and that to keep the data on screen updating smoothly this 2 way data binding should happen “in memory” like in example 1. But how to do this and at the same time taking care of the write to the DB?
Don’t we risk ending up duplicating state which is what the compose paradigm wants to avoid in the first place?
Any suggestion is very appreciated.alorma
07/02/2021, 2:36 PMLaunchEffect(myString) { store(myString) }
After myString = it
julioromano
07/02/2021, 2:38 PMalorma
07/02/2021, 2:45 PMjulioromano
07/02/2021, 3:12 PMCicero
07/02/2021, 3:30 PMCicero
07/02/2021, 10:48 PM