Skip to content

Convex for Swift 0.7.0

Latest

Choose a tag to compare

@dowski dowski released this 04 Dec 03:30

This is a minor feature release.

It includes a new API to observe the state of the underlying WebSocket connection to the Convex backend.

Here's a small example showing how to use watchWebSocketState() - it changes the color of an icon from blue to red depending on whether the WebSocket is connected or connecting. The Convex client will always try and connect the WebSocket so it will always be in one of those two states.

import SwiftUI
import ConvexMobile
internal import Combine

let convex = ConvexClient(deploymentUrl: "$URL")

struct ContentView: View {
  @State private var isConnected: Bool = false
  
  var body: some View {
    VStack {
      Image(systemName: "globe")
        .imageScale(.large)
        .foregroundStyle(isConnected ? (Color.blue) : Color.red)
    }
    .task {
      for await value: WebSocketState in convex.watchWebSocketState().values {
        switch value {
	        case .connected:
	          isConnected = true
	        case .connecting:
	          isConnected = false
        }
      }
    }
    .padding()
  }
}