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

How to cast [Record] to [Account] in query result #52

Closed
auctifera-josed opened this issue Dec 6, 2017 · 1 comment
Closed

How to cast [Record] to [Account] in query result #52

auctifera-josed opened this issue Dec 6, 2017 · 1 comment
Assignees
Labels

Comments

@auctifera-josed
Copy link

I'm using custom model objects with multiple queries, so I declared it like:

first {
    let queries = ["SELECT Name FROM Account", "SELECT Id FROM Contact", "Select Owner.Name FROM Lead"]
    return salesforce.query(soql: queries)
}.then { (results: [QueryResult<Record>]) -> () in
   let accounts = results[0].records
   let contacts = results[1].records
   let leads = results[2].records
}.catch { error in
   
}

My problem is that all the objects are 'Record' and not either 'Account', 'Contact' or 'Lead', I tried a simple let accounts = results[0].records as? [Account] which gives me a warning "Cast from '[Record]' to unrelated type '[Account]' always fails"

@mike4aday mike4aday self-assigned this Dec 6, 2017
@mike4aday
Copy link
Owner

mike4aday commented Dec 6, 2017

@auctifera-josed the issue is with Swift generics. QueryResult is defined as:
QueryResult<T: Decodable>: Decodable

So you could substitute one of your Decodable model objects for Record e.g. QueryResult<Account> -- but I don't see how you could use 3 different Decodable types simultaneously. There may be a way via custom decoding and protocol inheritance, but seems like too much, unnecessary effort.

Try instead to create 3 separate queries and use the fulfill method for parallel execution -- see line 24 in Globals.swift

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

No branches or pull requests

2 participants