Android
Data objects

Data Types

24min

Parse Data Types on Android

Introduction

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:

1 score: 1337, playerName: "Sean Plott", cheatMode: false

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.

Goal

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:

Document image


Prerequisites

To complete this tutorial, we need:

Understanding our App

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.

Let’s get started!

1 - Create App Template

Define the following variables in the MainActivity and replace the code in the onCreate method with the following code.

Java
Kotlin


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.

2 - Code for Save Object

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.

Java
Kotlin


3 - Code for Read 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.

Java
Kotlin


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.

Java
Kotlin


This is the showDataTypes function.

Java
Kotlin


4 - Code for Update Object

The updateObject function is responsible for updating data in the object created on the saveDataTypes function. We are using objectId again for update object.

Java
Kotlin


5 - Using Counters

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:

Java
Kotlin


6 - Using Lists

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.

6.1 - Examples with add and addAll

listStringField has the value:

["a","b","c","d","e","f","g","g"]

Running the code below:

Java
Kotlin


After this command the result of the stringList field will be:

["a","b","c","d","e","e","f","g","g"]

6.2 - Examples with addUnique and addAllUnique

listStringField has the value:

["a","b","c","d","e"]

Running the code below:

Java
Kotlin


After this command the result of thestringList field will be:

["a","b","c","d","e","f"]

No values were repeated.

6.3 - Examples with removeAll

listStringField has the value:

["a","b","c","d","e","f"]

Running the code below:

Java
Kotlin


After this command the result of thestringList field will be:

["a","b"]

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.

7 - Remove single field from ParseObject

You can delete a single field from an object by using the remove operation:

Java
Kotlin


It’s done!

At this point, we have learned Parse Data Types on Android.