iOS
...
Parse Swift SDK
Data Objects

Basic Queries

15min

Basic Queries

Introduction

In most use cases, we require to fetch data from a database with certain conditions. These conditions may include complex comparisons and ordering requirements. Thus, in any application, it is fundamental to construct efficient queries and, at the same time, the database has to be able to execute them as fast as possible.

The ParseSwift SDK does provide the necessary tools for you to construct any query according to the application requirements. In this tutorial, we explore these tools and use them in a real-world application.

This tutorial uses a basic app created in Xcode 12 and iOS 14.

At any time, you can access the complete Project via our GitHub repositories.

Goal

  • To understand how to create basic queries to retrieve data from a Back4App Database.

Prerequisites

To complete this quickstart, you need:

Understanding our Constacts App

The project template is a Contacts App where the user adds a contact’s information to save it on a Back4App Database

Document image


On the app’s homescreen you will find a set of buttons for different types of queries. Using the + button located on the top-right side of the navigation bar, we can add as many Contacts as needed.

Quick reference of commands we are going to use

For this example, we use the object Contact

Swift


The following methods will allows us to save and queryContactobjects:

Create contact
Query all
Query by name
Query by friend count
Query with ordering


1 - Download the Contacts App Template

The XCode project has the following structure

Document image


At any time, you can access the complete Project via our GitHub repositories.

To focus on the main objective of this guide, we will only detail the sections strictly related to queries and the ParseSwift SDK.

2 - Additional CRUD flow

Before getting started with queries, it is necessary to have some contacts already saved on your Back4App Database. In the NewContactController class, we implement a basic form to add a Contact. To save an instance of a Contact object, we use the handleAddContact() method implemented in the NewContactController class

Swift


For more details about this step, you can go to the basic operations guide.

3 - Performing basic queries

- By name

The first example we look at is a query that allows us to retrieve contacts which have a specific substring in their name field. In order to do this, we first create a QueryConstraint object. This object will contain the constraint we want. The ParseSwift SDK provides the following methods to (indirectly) create a QueryConstraint

Swift


For instance, a query that allows us to retrieve all the Contact’s with John in their name field can be created with

Swift


In case the constraint requires the name field to match exactly a given string, we can use

Swift


- By number of friends

A query with a constraint involving a numerical comparison can be constructed by creating aQueryConstraint with

Swift


To query all contacts with 30 or more friends, we use

Swift


- Ordering query results

For ordering the results from a query, the Query<contacts> object provides the method order(_:) which returns a new Query<contact> object considering the requested ordering option. As a parameter, we pass an enumeration (Query<contact>.Order) to indicate the ordering we want. The following snippet applies a descending order based on the birthday field

Swift


In the project example, we implemented the queries mentioned above. The ContactsController class has the method fetchContacts() where you will find the following snippet

Swift


4 - Run the app!

Before pressing the run button on XCode, do not forget to configure your Back4App application in the AppDelegate class!

Using the+button in the navigation bar, add a counple of contacts and test the different queries.

Updated 28 Mar 2024
Did this page help you?