You retrieve data from the database with the TinyDB.GetValue block. When you call
GetValue, you request particular data by providing a tag. For the No Texting While
Driving app, you can request the custom response using the same tag as we used
in the StoreValue, “responseMessage.” The call to GetValue returns the data, so you
must plug it into a variable.
Often, you’ll retrieve data from the database when the app opens. App Inventor
provides a special event handler, Screen.Initialize, which is triggered when the
app starts up. The general pattern is to call GetValue, put the returned data into a
variable, and then check to see if the database indeed returned some information.
This check is important, because generally the first time you run the app, there is no
database data yet (e.g., the first time No Texting While Driving runs, the user hasn’t
yet entered a custom response).
The blocks in Figure 22-2, for the Screen.Initialize of No Texting While Driving, are
indicative of how many apps will load data on initialization.
The blocks put the data returned from GetValue into the variable response and then
check if response has a length greater than 0. If it does, then the database did return
a nonempty custom response, and it should be put in the ResponseLabel. If the
length of the value returned is 0, it means no data with a tag of “responseMessage”
has been stored, so no action is necessary