Program with Xojo from scratch: persistence of data

Program with Xojo from scratch: persistence of data

We continue to advance in building our cross-platform application with Xojo for personal expense management; and in this chapter we will deal with solving one of the most important problems we have had so far: saving the inserted data on disk in order to be able to recover them after each new execution of the program.

This allows us to present another feature that Xojo greatly facilitates: access, read and write to any file that is on our storage devices.



For this we will use three new realbasic Framework classes (ie the set of ready-to-use "tools" provided by the development environment): FolderItem (https://docs.xojo.com/FolderItem), TextOutputStream (https: // docs.xojo.com/TextOutputStream) and TextInputStream (https://docs.xojo.com/TextInputStream).

Think of FolderItem as the class that, through its properties and methods, allows us to access any file, whether it already exists or whether it is a new file we want to create… as well as folders and / or directories.

In fact, through the properties available in this class we can carry out such common checks as checking if the file already exists in the expected path, if it is a folder, the modification or creation date, if it can be read or written on the destination indicated by the path, modify his permits ... and a long one and so on.

On the other hand, some of the methods available in the FolderItem class have the task of making it easier for us to copy the reference file to a new location, open it, delete it, move it… or create new folders / directories in the specified path.

As for the pair of TextInputStream / TextOutputStream classes, these have the task of working with a specific file (an instance or a FolderItem object), so that we can access its content as if it were a data stream.



Note that both classes start their names with the word "Text", which means that the data to be accessed will be treated as text rather than "binary" format.

In our case this is what we want and it works for us because all the information we are going to save and (subsequently) retrieve is in text format: name of the expense, date of purchase, amount, etc.

How is a "data stream" or "stream" different from a file? Think of the file as the data container, while the class in charge of accessing the data stored by the file would act on the content itself.

Through its properties and methods it is possible to move to a certain position (in bytes) within the file, as long as it does not exceed its length, of course. You can also read the contents of the "text" file line by line, all at once, or the fragment you want from the current position at the specified length.

For its part, the class that performs the opposite functions is TextOutputStream. With it, you can open a file (or create it, as long as you pass a reference to a FolderItem anyway) to download the content you want as text on it: line by line, a certain snippet, or all at once.

In practice

In the development of our sample application we will use these three classes to save both the categories and all the purchase items that we have introduced.

Throughout the video we focus on the details of how to save this information and retrieve it later using two files: one tasked with holding the name of the categories and the other where the associated expenses for each of the categories are located.


It is interesting because we also put into practice a lot of what we have seen so far: simple variables, and also collections of data (Arrays and Dictionaries); so it is a good time to refresh the knowledge of the previous two chapters… and also to see a little more in depth how to use them in practice.


App name and other details

At the beginning of the video we can also see something really important while creating any of our applications: how to indicate from the Xojo IDE the name we want to give our application… and also decide which architecture we want to compile it for!


You already know that, with Xojo, deciding whether you want your application to be compiled for Intel-based Macs, M1 processors, or as a Universal application is as simple as selecting the option from a menu.

Twitter: @xojoes

Facebook:  http://facebook.com/xojoes/

Download the latest version from www.xojo.com

Xojo Resources in Spanish: https://docs.xojo.com/spanish

add a comment of Program with Xojo from scratch: persistence of data
Comment sent successfully! We will review it in the next few hours.