Data Types
In this guide, you will learn about the Parse Datatypes using Android. You will read and save the Parse Objects on Back4App from an Android App.
Storing data on Parse is built around the ParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that we don’t need to specify ahead of time what keys exist on each ParseObject. We can set whatever key-value pairs we want, and our backend will store them. For example, let’s say we’re tracking high scores for a game. A single ParseObject could contain:
Keys must be alphanumeric strings, and values can be:
- String=> String
- Number(primitive numeric values such as int,double)
- Bool=> boolean
- DateTime=> java.util.Date
- Null=> JSONObject.NULL
- Array=>JSONArray
- File=> Parse File
- Pointer=> other ParseObject
- Relation=> ParseRelation
- Geopoint=> ParseGeoPoint
Each ParseObject has a class name that we can use to distinguish different sorts of data. For example, we could call the high score object a GameScore. There are also a few fields we don’t need to specify that are provided as a convenience:
- objectIdis a unique identifier for each saved object.
- createdAtand updatedAtrepresent the time that each object was created and last modified in the cloud.
Each of these fields is automatically filled in by Back4app at the moment we save a new ParseObject.
We recommend you NameYourClassesLikeThis (UpperCamelCase) and nameYourKeysLikeThis (lowerCamelCase), just to keep your code looking pretty.
This tutorial uses a basic app created in Android Studio 4.1.1 with buildToolsVersion=30.0.2 , Compile SDK Version = 30.0.2 and targetSdkVersion 30
At any time, you can access the complete Project via our GitHub repositories.
Our goal is to create an Android App that can process all data types provided by Parse Server. Here is a preview of what we are gonna achive:
To complete this tutorial, we need:
- An app created on Back4App.
- An android app connected to Back4App.
- Note: Follow the Install Parse SDK tutorial to create an Android Studio Project connected to Back4App.
You will create an app for a better understanding of Parse Data Types. In this Android app, you will create all data types in a class named DataTypes and assign values to this classes variables. Then we will read and update this data.
Note The data types Pointer, Relation, File, Geopoint will be covered later in specific guides.
Define the following variables in the MainActivity and replace the code in the onCreate method with the following code.
Before next steps, we need to connect Back4App to our application. You should save the appId and clientKey from the Back4App to string.xml file and then init Parse in our App.java or App.kt file. Follow the New Parse App tutorial if you don’t know how to init Parse to your app.
The create function will create a new object in Back4app database. We define the saveDataTypes function that we call in the onCreate function and use the following code in this function. We will save all data types with this function to the DataTypes classes object.
We will give the objectId variable that we have assigned in the saveDataTypes function as a parameter to the query and read the data that we have saved in the saveDataTypes function with the readObjects function.
In this section, we have created a model class called Data. We use the data we get in the readObjects function to create objects from this model class. We give these objects as elements to the list we created in Data type. Then we give this list as a parameter to the showDataTypes function and list it in the AlertDialog.
This is the Data model.
This is the showDataTypes function.
The updateObject function is responsible for updating data in the object created on the saveDataTypes function. We are using objectId again for update object.
The above example contains a common use case. The intField field can be a counter that we’ll need to update continually. The above solution works, but it’s cumbersome and can lead to problems if we have multiple clients trying to update the same counter. Parse provides methods that atomically increment any number field to help with storing counter-type data. So, the same update can be rewritten as:
Parse also provides methods to help in storing list data. There are three operations that can be used to change a list field atomically:
- setAddand setAddAll: append the given objects to the end of an array field.
- setAddUniqueand setAddAllUnique:add only the given objects which aren’t already contained in an array field to that field. The position of the insert is not guaranteed.
- removeand removeAll: removes all instances of the given objects from an array field.
listStringField has the value:
Running the code below:
After this command the result of the stringList field will be:
listStringField has the value:
Running the code below:
After this command the result of thestringList field will be:
No values were repeated.
listStringField has the value:
Running the code below:
After this command the result of thestringList field will be:
Note that it is not currently possible to atomically add and remove items from an array in the same save. We will have to call the save for every different array operation we want to perform separately.
You can delete a single field from an object by using the remove operation:
At this point, we have learned Parse Data Types on Android.