Attached is a simple app titled "My Diary". It lets you write a diary entry and store the text under the date/time of its creation.
It also has a ListPicker component that displays all the existing entries in the diary. By selecting one of these entries, the date and text are shown in a new screen. See these three images:
The app demonstrates how to make use of TinyDB in a special circumstance: when you need to save a list of tags.
There are two different things you need to store to TinyDB. One is the pair (date/time, your_text). However, since you will have many entries each tagged by the date/time value, in order to get back to old entries, you will need to constantly update a list that stores all these tags and store the whole list in the TinyDB. See blocks screenshot for the Save button.
The problem (that some of you have already encountered) is that at the first time you open you app, there are no entries stored under the tag "MyEntriesList". TinyDB doesn't give you an error, it simply return something called an empty string. But you are storing that value in a variable which you assume to be a list, and that you use as a list in the SaveButon.Click procedure. And this is when the app crashes, because you cannot add items to an empty string.
Here is how the problematic code looks like. You are retrieving the value stored with the tag "MyEntriesList", but that returns an empty string the very first time your app executes.
We need to fix this bug. The way to fix it is to make sure that we return a list when we ask TinyDB to give us back "MyEntriesList" value.
You can create a procedure that returns a value to do that. Here is the code of the procedure and how you can call it to fix the bug:
Warning: The persistent aspect of TinyDB storage only works when the app is downloaded onto the phone and launched as a regular application. When the application is launched from the BlocksEditor when the phone is tethered to the computer by a USB cable, the TinyDB storage is not persistent. For example, if you launch your app from the BlocksEditor , stop it, and then relaunch it from the BlocksEditor, it will not remember the values stored in TinyDB from the first launch. In order to test that an app with TinyDB works properly, you must download the app onto the phone and launch the app from the phone.