Connect your Swift & SwiftUI apps directly to a managed Google CloudSQL (PostgreSQL) database.
This repository contains the official open-source Swift SDK for Firebase Data Connect, a service that lets you build modern, data-driven applications on Apple platforms (iOS, macOS, etc.) with the power and scalability of a SQL database.
This SDK is perfect for those:
async/await for clean, concurrent code.@Observable Queries automatically update your SwiftUI views when data changes, making it incredibly simple to build reactive UIs.This guide will walk you through setting up a new iOS (or other Apple platform) project with Firebase Data Connect.
First, clone this repository to your local machine. This contains both the core SDK and the command-line tools needed for code generation.
git clone https://github.com/firebase/data-connect-ios-sdk.git
data-connect-ios-sdk folder you just cloned.FirebaseDataConnect library to your app's primary target.The Data Connect tools run on your Mac to provide a local development emulator and code generation service.
Start FDC Tools target and click OK.Start FDC Tools scheme. Go to Run > Options and check "Use custom working directory". Set this to the root folder of your Xcode project.Start FDC Tools scheme with My Mac as the destination and click Run (▶). This will open the FDC tools in your web browser.The tools will generate a custom Swift package based on your database schema.
.gql files) in your project's dataconnect subfolder and generate a new Swift package in a dataconnect-generated folder.dataconnect/default/connector.yaml file to specify the location of the cloned data-connect-ios-sdk by updating the coreSdkPackageLocation property.dataconnect-generated folder. Add this new library (e.g., ItemData) to your app target.GoogleService-Info.plist thats in your Xcode project folder and add it as a Reference.In your main app file (the one with @main), initialize Firebase and configure Data Connect to use the local emulator.
// MyApp.swift
import SwiftUI
import Firebase
import FirebaseDataConnect
import ItemData // The name of your generated SDK package
@main
struct MyApp: App {
init() {
// 1. Configure Firebase
FirebaseApp.configure()
// 2. Point Data Connect to the local emulator
DataConnect.itemsConnector.useEmulator()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
Now you can write data to the database from any SwiftUI view.
// ContentView.swift
import SwiftUI
import FirebaseDataConnect
import ItemData
struct ContentView: View {
var body: some View {
VStack {
Button("Create Item") {
Task {
do {
let itemID = UUID()
let itemName = "Item-\(Int.random(in: 1...1000))"
let result = try await DataConnect.itemsConnector.createItemMutation.execute(id: itemID, name: itemName)
print("✅ Successfully created item: \(result)")
} catch {
print("❌ Error creating item: \(error)")
}
}
}
}
}
}
Note: For macOS apps, you may need to enable App Sandbox -> Outgoing Connections (Client) for your Xcode app target.
Use a QueryRef to fetch data and automatically bind it to your SwiftUI view.
// ContentView.swift
import SwiftUI
import FirebaseDataConnect
import ItemData
struct ContentView: View {
// A reference to our query
@State var itemsQueryRef = DataConnect.itemsConnector.listItemsQuery.ref()
var body: some View {
VStack {
// (Create Button from previous step)
// The List will update when the query data changes
if let items = itemsQueryRef.data?.items {
List(items) { item in
Text(item.name)
}
}
Button("Refresh List") {
Task {
// Manually refetch the data
_ = try? await itemsQueryRef.execute()
}
}
}
.task {
// Fetch initial data when the view appears
_ = await itemsQueryRef.execute()
}
}
}
That's it! You've connected your app to a local SQL database, created a new record, and displayed a list of records in your UI.
schema.gql, queries.gql and mutations.gql to add a price field to the Item entity. The generated SDK should get automatically created. Hint: See comments in the files.Please see the Contributing guide for more information.
This repository is licensed under the Apache License, version 2.0. Your use of Firebase is governed by the Terms of Service for Firebase Services.