Execute SalesForce SOQL queries synchronously using iOS MobileSDK

168 Views Asked by At

I am using the latest Salesforce Mobile SDK to develop an iOS app to interact with CRM.

I have 2 SOQL queries to execute synchronously so that the data retrieved from first query can be used in the second query. The Salesforce Mobile 6.1 had an option something like:

    let restApi  = SFRestAPI.sharedInstance()
 restApi.Promises.query(soql: "SELECT Id,FirstName,LastName FROM User")
 .then { request in
    restApi.Promises.send(request: request)
 }
 .done { sfRestResponse in
    restResponse = sfRestResponse.asJsonDictionary()
    ...
 }
 .catch { error in
   //handle error
 }

But in the latest SDK the Promises have been deprecated. What is the better way to execute SOQL queries synchronously using the latest SalesForce Mobile SDK.

1

There are 1 best solutions below

0
On

Version 6.2 of the Salesforce iOS SDK uses PromiseKit to synchronously chain asynchronous tasks (such as web requests). Even though it is not supported out-of-the-box in their 7.0 SDK, you can still import it for use in your project: PromiseKit

If you want to accomplish the same functionality natively using the new 7.0 SDK, the simplest way is to chain the callbacks:

let queryRequestOne = RestClient.sharedInstance().buildQueryRequest(soql:"SELECT ... FROM ...")

RestClient.sharedInstance().send(request: queryRequestOne, onFailure: { (error, urlResponse) in
    SFSDKLogger.sharedInstance().log(type(of:self), level:.debug, message:"Error invoking: \(queryRequestOne)")
}) { [weak self] (response, urlResponse) in

    //Parse data from web response
    //..

    let queryRequestTwo = RestClient.sharedInstance().buildQueryRequest(soql:"SELECT ... FROM ...")

    RestClient.sharedInstance().send(request: queryRequestTwo, onFailure: { (error, urlResponse) in
        SFSDKLogger.sharedInstance().log(type(of:self), level:.debug, message:"Error invoking: \(queryRequestTwo)")
    }) { [weak self] (response, urlResponse) in

        //Parse data from web response and update UI on main thread

    }
}