iOS

Relational Queries

Introduction

In the previous guide we detailed how we can perform miscellaneous queries on a Back4App Database. In this guide we focus on a specific type of query that involves objects with relations.

Prerequisites

To complete this tutorial, you will need:

Goal

Query relational data stored on a Back4App Database using the ParseSwift SDK.

Step 1 - Quick review about the Query<U> class

Any query performed on a Back4App Database is done via the generic class Query<U>. The generic parameter U (conforming to the ParseObject protocol) is the data type of the objects we are trying to retrieve from the database.

Given a data type like MyObject, we retrieve these objects from a Back4App Database in the following way

1
2
3
4
5
6
7
8
9
10
11
12
import ParseSwift

struct MyObject: ParseObject {
  ...
}

let query = MyObject.query()

// Executes the query asynchronously
query.find { result in
  // Handle the result (of type Result<[MyObject], ParseError>)
}

You can read more about the Query<U> class here at the official documentation.

Step 2 - Save some data on a Back4App Database

Before we begin to execute queries, it is necessary to set up some data on a Back4App Database. We will store five types of objects:

1
2
3
4
struct Author: ParseObject {
  ...
  var name: String?
}
1
2
3
4
5
6
struct Book: ParseObject {
  ...
  var title: String?
  var publisher: Publisher?
  var publishingDate: Date?
}
1
2
3
4
5
struct ISBD: ParseObject {
  ...
  var isbn: String?
  var book: Pointer<Book>?
}
1
2
3
4
struct Publisher: ParseObject {
  ...
  var name: String?
}
1
2
3
4
struct BookStore: ParseObject {
  ...
  var name: String?
}

Additionally, in order to construct queries for relational data, we will implement the following relations

  • 1:1 relation between Book and ISBD.
  • 1:N relation between Book and Publisher.
  • M:N relation between Book and Author.
  • M:N relation between BookStore and Book.

We now proceed to store some data on the Back4App Database. This step can be implemented using Swift or directly from your app’s console on the Back4App platform.

After setting up your XCode project, calling the following method should save some sample data on your Back4App Database

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import ParseSwift

func saveSampleData() {
  do {
    // Authors
    let aaronWriter = try Author(name: "Aaron Writer").save()
    let beatriceNovelist = try Author(name: "Beatrice Novelist").save()
    let caseyColumnist = try Author(name: "Casey Columnist").save()
            
    // Publishers
    let acaciaPublishings = try Publisher(name: "Acacia Publishings").save()
    let birchDistributions = try Publisher(name: "Birch Distributions").save()
            
    // Books with their corresponding ISBD
    let aLoveStoryBook = try Book(
      title: "A Love Story",
      publisher: acaciaPublishings,
      publishingDate: Date(string: "05/07/1998")
    ).save()
    .relation?.add("authors", objects: [aaronWriter]).save() // Establishes the M:N relatin between Book and Author

    // Fetches the ISBD associated to aLoveStoryBook and establishes the 1:1 relation
    if let book = aLoveStoryBook, var isbd = try book.fetch(includeKeys: ["isbd"]).isbd  {
      isbd.book = try Pointer<Book>(book)
      _ = try isbd.save()
    } else {
      fatalError()
    }
            
    let benevolentElvesBook = try Book(
      title: "Benevolent Elves",
      publisher: birchDistributions,
      publishingDate: Date(string: "11/30/2008")
    ).save()
    .relation?.add("authors", objects: [beatriceNovelist]).save() // Establishes the M:N relatin between Book and Author

    // Fetches the ISBD associated to benevolentElvesBook and establishes the 1:1 relation
    if let book = benevolentElvesBook, var isbd = try book.fetch(includeKeys: ["isbd"]).isbd  {
      isbd.book = try Pointer<Book>(book)
      _ = try isbd.save()
    } else {
      fatalError()
    }
            
    let canYouBelieveItBook = try Book(
      title: "Can You Believe It",
      publisher: birchDistributions,
      publishingDate: Date(string: "08/21/2018")
    ).save()
    .relation?.add("authors", objects: [aaronWriter, caseyColumnist]).save() // Establishes the M:N relatin between Book and Author

    // Fetches the ISBD associated to canYouBelieveItBook and establishes the 1:1 relation
    if let book = canYouBelieveItBook, var isbd = try book.fetch(includeKeys: ["isbd"]).isbd  {
      isbd.book = try Pointer<Book>(book)
      _ = try isbd.save()
    } else {
      fatalError()
    }

    // Book store
    guard let safeALoveStoryBook = aLoveStoryBook,
          let safeBenevolentElvesBook = benevolentElvesBook,
          let safeCanYouBelieveItBook = canYouBelieveItBook
    else {
      throw NSError(
        domain: Bundle.main.description,  
        code: 0,
        userInfo: [NSLocalizedDescriptionKey: "Failed to unwrapp stored books."]
      )
    }

    // Saves the stores together with their 1:N relation with Book's 
    let booksOfLove = try BookStore(name: "Books of Love").save()
    _ = try booksOfLove.relation?.add("books", objects: [safeALoveStoryBook]).save()
            
    let fantasyBooks = try BookStore(name: "Fantasy Books").save()
    _ = try fantasyBooks.relation?.add("books", objects: [safeBenevolentElvesBook]).save()
            
    let generalBooks = try BookStore(name: "General Books").save()
    _ = try generalBooks.relation?.add("books", objects: [safeALoveStoryBook, safeCanYouBelieveItBook]).save()
            
  } catch let error as ParseError {
    print("ERROR:\n", error.message)
  } catch {
    print("ERROR:\n", error.localizedDescription)
  }
}

A quick way to insert elements on your Back4App Database is via the console located in your App’s API section. Once you are there, you can start running Javascript code to save the sample data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Authors
const aaronWriter = new Parse.Object('Author');
aaronWriter.set('name', 'Aaron Writer');
await aaronWriter.save();

const beatriceNovelist = new Parse.Object('Author');
beatriceNovelist.set('name', 'Beatrice Novelist');
await beatriceNovelist.save();

const caseyColumnist = new Parse.Object('Author');
caseyColumnist.set('name', 'Casey Columnist');
await caseyColumnist.save();

// Publishers
const acaciaPublishings = new Parse.Object('Publisher');
acaciaPublishings.set('name', 'Acacia Publishings');
await acaciaPublishings.save();

const birchDistributions = new Parse.Object('Publisher');
birchDistributions.set('name', 'Birch Distributions');
await birchDistributions.save();

// Books with their corresponding ISBD
const aLoveStoryISBD = new Parse.Object('ISBD');
aLoveStoryISBD.set('isbn', '9781401211868');
await aLoveStoryISBD.save();

const aLoveStoryBook = new Parse.Object('Book');
aLoveStoryBook.set('title', 'A Love Story');
aLoveStoryBook.set('publisher', acaciaPublishings);
aLoveStoryBook.set('publishingDate', new Date('05/07/1998'));
aLoveStoryBook.set('isbd', aLoveStoryISBD);
const bookARelation = aLoveStoryBook.relation("authors");
bookARelation.add(aaronWriter);
await aLoveStoryBook.save();
aLoveStoryISBD.set('book', aLoveStoryBook.toPointer());
await aLoveStoryISBD.save();

const benevolentElvesISBD = new Parse.Object('ISBD');
benevolentElvesISBD.set('isbn', '9781401211868');
await benevolentElvesISBD.save();

const benevolentElvesBook = new Parse.Object('Book');
benevolentElvesBook.set('title', 'Benevolent Elves');
benevolentElvesBook.set('publisher', birchDistributions);
benevolentElvesBook.set('publishingDate', new Date('11/31/2008'));
benevolentElvesBook.set('isbd', benevolentElvesISBD);
const bookBRelation = benevolentElvesBook.relation("authors");
bookBRelation.add(beatriceNovelist);
await benevolentElvesBook.save();
benevolentElvesISBD.set('book', benevolentElvesBook.toPointer());
await benevolentElvesISBD.save();

const canYouBelieveItISBD = new Parse.Object('ISBD');
canYouBelieveItISBD.set('isbn', '9781401211868');
await canYouBelieveItISBD.save();

const canYouBelieveItBook = new Parse.Object('Book');
canYouBelieveItBook.set('title', 'Can You Believe It?');
canYouBelieveItBook.set('publisher', birchDistributions);
canYouBelieveItBook.set('publishingDate', new Date('08/21/2018'));
canYouBelieveItBook.set('isbd', canYouBelieveItISBD);
const bookCRelation = canYouBelieveItBook.relation("authors");
bookCRelation.add(aaronWriter);
bookCRelation.add(caseyColumnist);
await canYouBelieveItBook.save();
canYouBelieveItISBD.set('book', canYouBelieveItBook.toPointer());
await canYouBelieveItISBD.save();

// Book store
const booksOfLoveStore = new Parse.Object('BookStore');
booksOfLoveStore.set('name', 'Books of Love');
const bookStoreARelation = booksOfLoveStore.relation("books");
bookStoreARelation.add(aLoveStoryBook);
await booksOfLoveStore.save();

const fantasyBooksStore = new Parse.Object('BookStore');
fantasyBooksStore.set('name', 'Fantasy Books');
const bookStoreBRelation = fantasyBooksStore.relation("books");
bookStoreBRelation.add(benevolentElvesBook);
await fantasyBooksStore.save();

const generalBooksStore = new Parse.Object('BookStore');
generalBooksStore.set('name', 'General Books');
const bookStoreCRelation = generalBooksStore.relation("books");
bookStoreCRelation.add(aLoveStoryBook);
bookStoreCRelation.add(canYouBelieveItBook);
await generalBooksStore.save();

Step 3 - Query the data

Once the database has some sample data to work with, we start executing the different kinds of queries associated with the relations detailed earlier.

Queries involving 1:1 relations

Given two data types sharing a 1:1 relation (Book and ISBD in this case), we can retrieve one from the other as follows. The way we implemented the relation in Book allows us to retrieve its related ISBD object simply by calling the include(_:) method on the query. Let us retrieve the ISBD from the book A Love Story:

1
2
3
4
5
6
7
let aLoveStoryBookQuery = Book.query("title" == "A Love Story").include("isbd") // Note how we include the ISBD with the include(_:) method

let book = try? aLoveStoryBookQuery.first() // Retrieves synchronously the book including its ISBD

aLoveStoryBookQuery.first { result in // Retrieves asynchronously the book including its ISBD
  // Handle the result (of type Result<Book, ParseError>)
}

On the other hand, a query to retrieve a Book object related to a given ISBD is implemented in the following way. By looking at the implementation of ISBD, we note that the relation is represented by the book property (of type Pointer<Book>). This pointer provides a set of methods and properties to retrieve information about the object it points to. In particular, we call the fetch(...) method on the book property to fetch the associated Book

1
2
3
4
5
6
7
let someISBD: ISBD

let book: Book? = try? someISBD.book?.fetch() // Retrieves synchronously the book asscociated to someISBD

someISBD.book?.fetch { result in // Retrieves asynchronously the book asscociated to someISBD
  // Handle the result (of type Result<Book, ParseError>)
}

We should remark that this implemetation for a 1:1 relation is not unique. Depending on your use case, you can implement 1:1 relations in different ways.

Queries involving 1:N relations

In a scenario where we need to query all the books published by a given publisher, we first need to retrieve the publisher. For instance, we first retrieve the data object associated with the publisher Acacia Publishings. Depending on the situation, this procces may vary

1
2
3
4
5
6
7
8
9
10
11
12
do {
  // Using the object's objectId
  let acaciaPublishings = try Publisher(objectId: "SOME_OBJECT_ID").fetch()

  // Or
  // Using a Query
  let acaciaPublishings = try Publisher.query("name" == "Acacia Publishings").first() // Returns (synchronously) the first Publisher with name 'Acacia Publishings'. The constraint is constructed using the == operator provided by the ParseSwift SDK

  ... // To be completed below
} catch {
  // Hanlde the error (of type ParseError)
}

Now that we have access to acaciaPublishings, we can construct the query to retrieve its related books. We proceed to create the query by instantiating a Query<Book> class. In this case, this class is instantiated using the static method query(...) provided by the Book object. The (variadic) arguments for this method are the standard QueryConstraint objects. Therefore, the books we are looking for are retrieved with the following snippet

1
2
3
4
5
6
7
8
9
10
11
12
do {
  let acaciaPublishings = try Publisher.query("name" == "Acacia Publishings").first() // Returns the first Publisher with name 'Acacia Publishings'

  let constraint: QueryConstraint = try "publisher" == publisher
  let query = Book.query(constraint) // Creates the query to retrieve all Book objects where its publisher field equalt to 'acaciaPublishings'

  let books: [Book] = try query.find() // Executes the query synchronously

  // books should contain only one element: the book 'A Love Story'
} catch {
  // Hanlde the error (of type ParseError)
}

An asynchronous implentation for the above snippet may be written in the following way

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// We retrieve the Publisher with name 'Acacia Publishings'
Publisher.query("name" == "Acacia Publishings").first { result in
  switch result {
  case .success(let publisher):
    guard let constraint: QueryConstraint = try? "publisher" == publisher else { fatalError() }

    // Then, we retrieve the books with the corresponding constraint          
    Book.query(constraint).find { result in
      switch result {
      case .success(let books):
        // books should contain only one element: the book 'A Love Story'
        break
      case .failure(let error):
        // handle the error (of type ParseError)
        break
      }
    }

  case .failure(let error):
    // handle the error (of type ParseError)
    break
  }
}

Queries involving M:N relations (Case 1)

To illustrate this case, we consider the following scenario; we want to list all the stores containing books published after a given date (e.g., 01/01/2010). Firstly we require an intermediate query to select the books. Next, we construct the main query to list the stores.

Therefore, we prepare the first query for the books

1
2
3
4
5
6
7
8
9
10
let booksQuery = Book.query("publishingDate" > Date(string: "01/01/2010")) // We construct the date constraint using the > operator provided by the ParseSwift SDK
        
do {
  let books = try booksQuery.find()
  ... // To be completed below
} catch let error as ParseError {
  // Handle any potential error
} catch {
  // Handle any potential error
}

We then construct the stores’ query using booksQuery’s results. The method containedIn(_:array:) returns the constraint we need for this case

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
let booksQuery = Book.query("publishingDate" > Date(string: "01/01/2010")) // We construct the date constraint using the > operator provided by the ParseSwift SDK
        
do {
  let books = try booksQuery.find()
  
  // Here is where we construct the stores' query with the corresponding constraint
  let storesQuery = BookStore.query(try containedIn(key: "books", array: books))
    
  let stores = try storesQuery.find()
    
  // stores should containt only one element: the 'General Books' BookStore 
} catch let error as ParseError {
  // Handle any potential error
} catch {
  // Handle any potential error
}

Similarly, we can implement this process asynchronously

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let booksQuery = Book.query("publishingDate" > Date(string: "01/01/2010")) // We construct the date constraint using the > operator provided by the ParseSwift SDK

booksQuery.find { result in
  switch result {
  case .success(let books):
    guard let constraint = try? containedIn(key: "books", array: books) else { fatalError() }
    let storesQuery = BookStore.query(constraint)
                
    storesQuery.find { result in
      switch result {
      case .success(let stores):
      
      case .failure(let error):
        // Handle the error (of type ParseError)
      }
    }
  case .failure(let error):
    // Handle the error (of type ParseError)
  }
}

Queries involving M:N relations (Case 2)

Suppose we need to select all the stores that have books written by a given author, say, Aaron Writer. In order to achieve this, we require two additional queries:

  • A query (Query<Author>) to obtain the object associated with the author Aaron Writer.
  • A query (Query<Book>) to select all the books written by Aaron Writer.
  • The main query (Query<BookStore>) to select the stores we are looking for.

The procedure to implement these queries are very similar to the previous ones:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let authorQuery = Author.query("name" == "Aaron Writer") // The first query to retrieve the data object associated to 'Aaron Writer'

do {
  let aaronWriter = try authorQuery.first()
            
  let booksQuery = Book.query(try containedIn(key: "authors", array: [aaronWriter])) // The second query to retrieve the books written by 'Aaron Writer'
            
  let books = try booksQuery.find()
            
  let storesQuery = BookStore.query(try containedIn(key: "books", array: books)) // The main query to select the stores where the author ('Aaron Writer') has his books available
  
  let stores = try storesQuery.find()
            
  // stores should contain two items: 'Books of Love' and 'General Books'
} catch let error as ParseError {
  // Handle the error
} catch {
  // Handle the error
}

Conclusion

With the ParseSwift SDK, we were able to construct relational queries that allowed us to select items based on the type of relations they have with other data types.