Using TinyDB in App Inventor

(This post was completely rewritten and updated on October 30, 2015)

What is TinyDB?

TinyDB is a simple “database” that stores data on your phone or tablet. Unlike program variables that go away when your app is finished running or your phone is re-set, values stored in TinyDB remain on your phone for use the next time your app is run.

About Memory on your Phone or Tablet

Your smart phone or tablet typically has two primary types of memory: RAM and FLASH memory.

RAM stands for “random access memory” – but today we mostly think of RAM as memory that can be accessed very fast (as compared to Flash or hard drive memory storage). RAM retains values as long as power is applied to the RAM circuity. Once we turn off the power, the values stored in RAM are lost. (In some applications, extra batteries are used to continuously provide power to RAM even when the “normal” power is turned off.)

Flash memory retains values when the power is turned off. But access to Flash RAM is not as fast as access to conventional RAM memory.

Why is it called “Flash”?  There was an early version of memory technology where the memory was erased by literally flashing it with ultraviolet light. However the inventor of Flash RAM chose the name “Flash” for different reasons. Modern Flash RAM is read, written and erased electronically.

App Inventor variables are stored in RAM memory – and the content of RAM is erased or reset whenever the power is turned off. TinyDB, on the other hand, stores values in FLASH RAM, where the values remain even when the power is turned off.

Using TinyDB

TinyDB provides a simple way to store and retrieve data efficiently and to store the data in long-term storage.  TinyDB is based on the concept of a “tag” to identify the stored data, and the data value. Think of a “tag” as like using your name as your identification to look up your address:

Tag value: Martin

Value: 123 Main St, Anytown, USA

or

Tag value: Alexa

Value: 321 Other St, Someplace, USA

TinyDB uses the “tag” (such as Alexa) to quickly locate the corresponding value. Even if you have 100 names and addresses stored in TinyDB, TinyDB can  look up the “tag” quickly and use the tag to find the corresponding value. We do not need to know how TinyDB does its look up so fast – it just does it [see Footnote 1].

In most database programs, the “tag” is known as a “key” or “key value”. App Inventor uses the name “tag” in place of “key”. As I am used to the name “key”, I tend to use “key” were I should have used “tag” in App Inventor! You will see this is the sample program, below!

To learn how to put TinyDB in operation, we construct a very simple app, described below.

Continue reading

AI Companion and your phone’s battery

On my phone, if I leave AI Companion running, the battery life of my phone goes down more rapidly.

Consequently, when I am done working on an app on my phone, I go in to Settings | Apps on the Android phone, find MIT App Inventor in the applications list, select it, and then select Force Stop.

I have no idea if other people experience this problem but the fix is simple – just kill the AI Companion app if its no longer needed.

This issues appears to be because the AI Companion links to the App Inventor cloud-based editor – as you edit and make changes, those changes are copied from the editor back to your device, almost in real time. However, if you walk away from the computer and the phone, neither realizes you left for a cup of coffee or to walk the dog – and they may continue to chat with each other, draining the battery.

Regardless, when developing apps for your phone or tablet, your best bet is to turn off battery saving features and leave your phone on (so you don’t have to continually enter your pass code or otherwise restart the phone after it goes to sleep). AND – very important – leave it plugged into a charger. Taking these steps can make your app development go more smoothly.

You can run App Inventor and install apps to your phone at Starbucks

As you already know, you can use App Inventor and the AI Companion on your phone to install apps from your computer (Browser) to your smart phone, as long as both are on the same network.

This works at Starbucks too 🙂 Just run appinventor.mit.edu to access your app in the cloud, run the MIT AI Companion app on your phone, and then select Connect | AI Companion in the browser window.

The app installs on to your phone.

App Inventor 2 – Tutorial #1 Introduction

This introduction is for those completely new to App Inventor – What is it? How does it work? Why should I be interested?

I have scripted out about 4 more, so far, but recording them may be delayed as construction crews are ripping up and repaving the road outside my office during the next few days.