What is data? We’ve already mentioned data quite a few times in previous lessons. But what is data specifically when it comes to application development? Data in app development is not just information, it is also the specific structure of the information.
This is important because of the different ways information can be used. For example, numbers can be summed up, but you can’t do the same with pieces of text. We’ll also need other types of data to represent lists and other collections of information.
Let’s start by considering the page you’re viewing this course. You have a mixture of text, images, and videos. There are also various different types of text and content elements. For us humans, parsing this kind of information is easy. We can seamlessly read over some text, look at images and move on to watch a video.
Data Must be Structured for Your Application to Use It
In an application, we must be aware of the different types of data and how the technology or users will interact with it. This means structuring information in a specific way. For a page, such as this course page, it means that, for the application, we must separate every piece of information according to its type and its place in the content hierarchy.
This requires the citizen developer to be able to imagine views, pages, and really anything as data structures. Imagine for a moment that the way you can describe anything is with a spreadsheet and you must only put one type of thing in a single column and label each column by category.
With this in mind, let's consider a contact list as an example. A contact list will contain names of people, and also their email addresses and phone numbers. People on the contact list might have multiple emails and phone numbers, of which one might be marked "primary". This is also a data structure.
At a minimum, in a contact list, we have a few different forms of text with names, emails, and phone numbers. We also have lists of emails and phone numbers. Within those lists, we might also have one item that is marked primary.
Seeing the real world as data structures is a skill that can only be acquired by spending time with it. It is opposed to our normal, intuitive, way of understanding the world. If you find yourself struggling with understanding data structures, then I recommend you try and plan your application in a spreadsheet or something similar. It can be easy to overlook data complexity, when pen and paper and text editors let us freely combine content and ideas.
Being able to think about our data structurally, requires us to know how applications classify things. Next, we’re going to look at some common ways to define data types in application development.
Common Data Types and Their Uses
Now, I want to discuss some of the common data types you will encounter and how they can be used.
First, we have simple types, text and number. Text is often also called a "string". Text type data is used to represent any kind of text the application contains. Numbers can also have subtypes based on the type of number such as "integer".
Numbers are used where we need do math with variables. Note that a sequence of numbers like 123 can equally be text or a number depending on the context. On the other hand, a sequence like 012 can be text, but it is not a number.
Typically, it is quite easy to convert numbers into text, which is often the case when you want to display dynamic numbers in a string of text for example.
Another simple data type is a Boolean or a true/false value. This is the type that expresses that something either is or isn’t true. This type is used in checks and toggles. For example, in our note-taking app, when we check if the length of the title is more than zero characters, the answer to this check is either true or false. True/false values are something you would use to create a toggle in your app, like for example toggling between darkmode and lightmode in the UI.
Text, number and a true/false Boolean are often called "simple data types". In app development, we also have so called complex data types.
The first of the complex types is a list. A list expresses a list of items, that have some position on the list. The items can be of any data type, a list may even be a list of lists. List type data can be processed in loops, where for example you could create a row in the UI for each item in a list. In many programming languages, a list is denoted by enclosing brackets, so this is something that you are likely to see as you develop applications.
The second complex type is an object. An object expresses a collection of related data. Whereas a list has items, an object has properties. For example, a piece of contact information can be expressed as an object that has the properties "name", "email" and "phone". Object properties can also be of any type, including lists and other objects.
An address book could be expressed as a list containing many contact information objects. These are just simple examples, but you can get an idea how just a few types can be used to express complicated data structures. Again, coming from programming languages, an object is often denoted by curly brackets.
This should give you a basic idea about what types of data can be stored in variables, or be input and output to and from functions.
There are comparatively few data types in use in applications, considering the huge variety of different kinds of data that can be represented. However, even using a small selection of data types, we can represent very complex data structures using a lists and objects.
Storing and Retrieving Data
So far, we’ve talked about variables and the possible data types contained within them. However, we also know that variables only exist while the application is running and are reset when the app is shut down. This means that we need a separate way of storing data when the application is not in use. This is where databases come in.
A database typically exists in the cloud, and can be accessed from the app across different devices. The approach to data stored in a database is slightly different from variables. In a database, we need to include the data that should be recalled between longer periods of time. For our note-taking app, this would be the list of notes.
The way to do this in application development is to define the data in the database, for the notes, this would be the Title and Content fields. When the note is saved in the app, the data from the variables in the app are combined and sent to the database as a record. A record is similar to an object, but, with databases, the items in the database are usually referred to as records or documents.
If these records are in the database, they can be retrieved, which is what we would do with the list of notes. The list would then be put in a variable, which would be bound (with a binding) to a list component in the UI.
The cloud is not the only place where we can persistently store data. On devices, we also have local storage. Essentially, local storage is a database on your device, only accessible by you.
The interaction with the local storage is to databases. You take some data from your variables and store them on the device, from where they can be recalled later.
There are many uses for this, but one of the important ones is offline capabilities. If your app doesn’t have internet access, it can still access the local storage. Offline storage might also be sometimes preferable for security or privacy reasons.
There are also performance considerations to using databases. If we can have data in variables and databases, why not just use a database? The issue is that variables live in the memory of the device. This means that, no matter how fast a database is, it will be slow compared to accessing a variable. This is why many applications spend time loading information at the start from the databases into the application variables.
The following are the main points of this lesson:
- Data must be structured appropriately in order for your application to know what to do with it.
- Seeing the real world as data structures is a skill that can only be acquired by spending time with it. If you find yourself getting stuck, try to organize the information in a spreadsheet first.
- Simple data is considered text, numbers and Boolean information.
- Text data, often called a string, is used to refer to any text in your app.
- Numbers data may also be further identified as an integer for example, and it is used for the mathematical calculations with your variables.
- Boolean data represents those with true or false variables as a result.
- Variable data is stored in memory, where it can be accessed quickly, but it is not preserved for long periods of time.
- Data in a database is slower to access, but is used for more complex data, and it persists for long periods of time.
A lot of the same data will flow through the variables of your app into a database. The main difference is the context for that data and how it is going to be preserved. The important takeaway is that data in an application is stored and retrieved using variables, and that the variables will typically contain much more data about the application state, whereas, in a database, we want to store and retrieve only the information necessary to have over the long term.