What's New

IBM Cloud Functions Adds Support for Swift 4.2

Share this post:

What’s happening with IBM Cloud Functions and Swift?

I’m excited to announce that IBM Cloud Functions officially supports Swift 4.2, which has been optimized for latency and local caching.

The new Swift 4.2 runtime leverages a new web proxy built with Go that replaces the previous Python web proxy. The web proxy allows action invocations to respond faster by providing lower latency for blocking calls. In addition, the web proxy provides some local caching on container re-use when hitting a warm invocation. Since the Swift program doesn’t exit after the invocation returns the result, the next invocation can take advantage of local caching such as global instances in memory or connections pools.

Ready to try it out?

Swift 4 offers the ability to use Codable types for input and output. In this example, the function takes a Codable “Input” and responds with a Codable “Output.”

  1. Create the following Swift file.

    struct Input: Codable {
    let name: String?
    }
    struct Output: Codable {
    let body: String
    }
    func main(param: Input, completion: (Output?, Error?) -> Void) -> Void {
    let result = Output(body: "Hello \(param.name ?? "IBM Cloud")!")
    completion(result, nil)
    }
  2. Create the helloSwift action.ibmcloud fn action update helloSwift main.swift --kind swift:4.2 --web true
  3. Invoke the action.

    ibmcloud fn action invoke helloSwift -r
    {
    "body": "Hello IBM Cloud!"
    }
  4. Get the URL for the web action.

    ibmcloud fn action get helloSwift --url

 

Using a single file is recommended only for the development of quick demos. For production, it is better to pre-compile your Swift action as this avoids large latency on cold starts. It also allows you to compile your code and leverage any third-party libraries and dependencies.

How much faster is Swift 4.2?

Swift 4.2 is much faster than Swift 3.1.1 or Swift 4.1 as you can see in the graph below comparing warm invocation performance. Start new projects using Swift 4.2 or migrate any existing actions.

How do I learn more?

To set up a project and compile your Swift 4.2 Actions, check out the IBM Cloud Functions documentation.

Senior Technical Staff Member (STSM) - IBM Cloud Functions

More What's New stories
May 3, 2019

Kubernetes Tutorials: 5 Ways to Get You Building Fast

Ready to start working with Kubernetes? Want to build your Kubernetes skills? The five tutorials in this post will teach you everything you need to know about how to manage your containerized apps with Kubernetes.

Continue reading

May 3, 2019

Using Portworx to Deploy and Manage an HA MySQL Cluster on IBM Cloud Kubernetes Service

This tutorial is a walkthrough of the steps involved in deploying and managing a highly available MySQL cluster on IBM Cloud Kubernetes Service.

Continue reading

May 2, 2019

Kubernetes v1.14.1 Now Available in IBM Cloud Kubernetes Service

We are excited to announce the availability of Kubernetes v1.14.1 for your clusters that are running in IBM Cloud Kubernetes Service. IBM Cloud Kubernetes Service continues to be the first public managed Kubernetes service to support the latest upstream versions from the community.

Continue reading