Firebase Realtime Database

Last Updated: 25 June 2022


Read Data-Custom: Get JSON String

Enabling "Get JSON String" will get Data in the Original JSON format of Firebase.

The "ProUI-GridView JSON" option allows you to get data in a format that is compatible with the well-liked ProUI GridView Extension by Aekiro. This option would be great for setting up smooth-scrollable list or Leaderboard easily.

IMAGE: Note The differences in format between the Original JSON String and the new ProUI-GridView JSON String.


Read Data-Custom: Get Array-JSON String

NOTE: This feature works along with the Realtime-Database PRO Plugin.

Description


You can read any Array Data stored in Firebase Database Console in this format below:

An example of how the data will look like in Firebase Console (left),
Read action result when using Get Array JSON String (middle)
Read action result when using Get JSON String (right)

An Array-JSON String can also contain data in json format in cases like this:

An example of how the data will look like in Firebase Console (left),
Read action result when using Get Array JSON String (
right)

How to Write Data in Array format


For this, you need the Realtime-Database PRO plugin.


Realtime-Database & the Array Plugin

You can also use Read Data-Custom's "Get Array-JSON String" option to get a json string that is supported by the Construct 3's Array Plugin.

This section describes when to and when not to use "Get Array-JSON String" option for Construct 3's Array Plugin.

When you don't need Array-JSON String


In usual cases, you can store Construct 3's Array.AsJSON data as a single string for example like this:

As you can see above, the data gets stored in string format. This can be simply be read using the Read Data (Custom) action like this:

However what is the problem with writing an Array data as a single string? The answer is, you have less control over the array data string. If you need to change a single data in that array string, you will need to rewrite the whole array. For example, you want to change "Messi" to "Neymar". To do this you would need to rewrite the whole string and that would cost you unnecessary data usage.

Use this method if you don't have the Realtime-Database PRO plugin.


When you need Array-JSON String

NOTE: This feature works along with the Realtime-Database PRO Plugin.

As opposed to the method used above, to have more control over the stored Array data, and to save data usage, you can use this option.

First use the Write/Replace JSON/Array Data action using the Array.AsJSON data to write an array in this format:

How to write Array's JSON data (left)
An example of how the data will look like in the Firebase Console (right)

As you can see above, the data gets stored in our required format. This can be be read using the Read Data (Custom) action like this:


Learn about Asynchronous Actions

The image above is a snapshot of the Firebase Realtime Database Action list.

Most Firebase functions such as reading or writing data requires some time to complete because it involves interactions with servers (such as loading data from a server or sending data to it).

The "arrow-around-clock" icon in the image indicates that the corresponding actions are Asynchronous. While such an action is still running (for eg. when some data is being fetched from the Firebase servers), other actions continue executing, one after the other.

Actions execute in the order they are placed in. A synchronous action will stop the next action from starting until it is completed. But an asynchronous action will allow the next action to start even when it is still executing.

But why all the trouble? Why are the Firebase actions asynchronous? This is because Synchronous actions are supposed to execute really fast- mostly within a few hundred microseconds. They happen so fast that you usually get a seamless experience. But in case of Asynchronous actions, loading data or writing takes at least a few seconds in a normal network speed and for that time duration you don’t want the next actions to stop executing. If your Asynchronous actions were forced to be synchronous, your game would have looked like it freezed. You don’t want that. So you can think of it like asynchronous actions execute in background in a different thread while the synchronous actions execute in-front, one by one.


How to Read Data from Firebase Realtime-Database

This example above is a wrong way to use a Database function.

In the example, “Read User Data” action is asynchronous, and “Set text” action is synchronous. Here, the user won’t get any text in the output (Output will be null). This is because, “Read User Data” will take some time to fetch data from the Firebase server, and as it is asynchronous, it will allow the next action, “Set text” to start even though it has not completed fetching.


So follow any of these three examples below according to your requirements:

1. You can use “On” trigger condition. On Data Changed conditions will be triggered every time there is any change in the Read data location. So, Sync data will work. (Recommended in most cases)

2. Conditions such as Data is Read will run every tick and hence, the text will also be set every tick.

3. “Wait for previous actions to complete” makes the next actions wait until the previous actions are completed.

NOTE- If you are using this method, Sync parameter must be set to False. “Wait for previous actions to complete” will work only if sync is disabled.