Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trouble Serializing Custom Objects From Search Response #873

Closed
Broich opened this issue Nov 10, 2024 · 2 comments
Closed

Trouble Serializing Custom Objects From Search Response #873

Broich opened this issue Nov 10, 2024 · 2 comments
Assignees

Comments

@Broich
Copy link

Broich commented Nov 10, 2024

Hello there,

I upgraded the Algolia Swift client to 9.9.1 and I'm having trouble understanding how I serialize response hits into my custom objects.

Previously, I was able to do something like this:

index?.search(query: query) { [weak self] result in
    switch result {
    case .failure(let error):
        self?.logger.error("Error returning search results: \(error)")
    case .success(let response):
        do {
            let results: [MyLocalObject] = try response.extractHits()
            self?.results = results
            DispatchQueue.main.async {
                self?.tableView.reloadData()
            }
        } catch let error {
            self?.logger.error("error: \(error)")
        }
    }
}

But extractHits seems to be gone now and I couldn't find a place in the documentation that would explain if and what it was replaced by.

I'm assuming I'm missing something super obvious.

Thanks for your help and support.

@Fluf22 Fluf22 self-assigned this Nov 10, 2024
@Fluf22
Copy link
Contributor

Fluf22 commented Nov 13, 2024

Hey @Broich 👋🏻
With the new client, you can leverage generics:

import Foundation
import Core
import Search

let appID = "YOU_APP_ID"
let apiKey = "YOUR_API_KEY"
let indexName = "YOUR_INDEX_NAME"

struct MyLocalObject: Codable {
    let title: String
    let original_language: String
    let genres: [String]
}

let client = try SearchClient(appID: appID, apiKey: apiKey)

let res: SearchResponse<MyLocalObject> = try await client.searchSingleIndex(indexName: indexName)
dump(res.hits)
image

Note

Your struct must conform to the Codable protocol

@Fluf22 Fluf22 closed this as completed Nov 13, 2024
@Broich
Copy link
Author

Broich commented Nov 15, 2024

Very helpful, thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants