SFRestAPI (Blocks) Category

For receiving and handling REST API responses, you can use inline code blocks instead of a delegate class. This alternative Objective-C approach lets you send a request and handle its asynchronous response in a single method call.

Mobile SDK for native iOS provides block methods for single requests, composite requests, and batch requests. When you use these methods, you provide block arguments to handle success and failure responses. Mobile SDK forwards the asynchronous response to your success or failure block according to the response’s network status.

Block methods and associated typedefs are defined in the SFRestAPI (Blocks) category as follows:

Each send method requires two blocks:

Failure Block

A failure block can receive timeout, cancellation, or error failures.

  • Block type:
  • Response type: NSError

Block type and response type depend on the request type as follows:

Single Request

  • Response type: NSArray or NSData object, depending on the data returned.
  • Block types (use the appropriate template for your request’s return type):
Composite Request

Batch Request

To send a request using a block method:

  1. Create the SFRestRequest object that fits your needs.

    • For a single request:

      • Create your SFRestRequest object by calling the appropriate SFRestAPI factory method.

      • Send your request. The following example handles any request that returns an NSDictionary response:

    • For batch and composite requests:

      1. For each subrequest, create an SFRestRequest object by calling an appropriate factory method.

      2. To create the SFRestRequest object that you send to Salesforce, pass an array of your subrequest objects to the batch or composite factory method. See SFRestAPI.h for more information.

  2. Design your success block to handle the expected data.

  3. Design your failure block to handle the issue gracefully.

  4. Pass the SFRestRequest object you created in step 1, and your success and failure blocks, to the appropriate send block method.

  • For Swift, Mobile SDK refines the Objective-C block methods to funnel the REST response into a single Swift completion closure. See Handling REST Responses.
  • In Objective-C, judicious use of blocks and delegates can help fine-tune your app’s readability and ease of maintenance. Ideal conditions for using blocks often correspond to those that mandate inline functions in C++ or anonymous functions in Java. Ultimately, you make a judgment call based on your own requirements.