sdk-for-apple: πŸ› Bug Report: Crash with Query select, AppwriteModels/Document.swift:94: Fatal error: Unexpectedly found nil

πŸ‘Ÿ Reproduction steps

When I attempt to perform a simple query operation which includes a select, the Appwrite code crashes

The setup is very simple; there’s a collection of Books as shown in the documentation. Each Book has two properties, title and author (again per the documentation).

The query is for document title matching Book A and I want to return only the title and author properties. (yes, I know that’s the only two but for this report it demonstrates the bug)

If in the below code the queries section is removed OR the select statement is removed, the query works properly.

The code:

func outputSampleBooks() async throws {
    do {
        let documentList = try await databases.listDocuments(
            databaseId: databaseId,
            collectionId: collectionId,
            queries: [
                Query.equal("title", value: "Book A"),
                Query.select(["title", "author"])
            ]
        )
    } catch {
        print(error)
    }
}

The books collection is attached below

and then the crash:

AppwriteModels/Document.swift:94: Fatal error: Unexpectedly found nil while unwrapping an Optional value

The crash is located in AppwriteModels->DocumentList.swift->from(map:)

public static func from(map: [String: Any] ) -> DocumentList {
        return DocumentList(
            total: map["total"] as! Int,
            documents: (map["documents"] as! [[String: Any]]).map { Document.from(map: $0) } //crash
        )
    }

Image

πŸ‘ Expected behavior

It should… not crash

πŸ‘Ž Actual Behavior

It actually crashes whenever the query includes a select statement

🎲 Appwrite version

Different version (specify in environment)

πŸ’» Operating system

Linux

🧱 Your Environment

AppWrite sdk-for-apple 9.0.1 XCode 16 macOS Sequoia 15.5

I’ve duplicated this on several workstations with similar setups

πŸ‘€ Have you spent some time to check if this issue has been raised before?

  • I checked and didn’t find similar issue

🏒 Have you read the Code of Conduct?

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

@ChiragAgg5k Queries with select is now working. Thank you so much!

The Book struct now looks like this: it has a structure where all properties (that may appear in a query select) are marked as optional

struct Book: Encodable, Decodable {
    var id: String
    let title: String?
    let author: String?

    enum CodingKeys: String, CodingKey {
        case id = "$id"
        case title = "title"
        case author = "author"
    }
}

and these all work

Query.equal("title", value: "Book A"),
Query.select(["title"])    //or this Query.select(["author"]) or this Query.select(["title", "author"])