Run embedded seekdb in an iOS app with Swift
Run seekdb inside a SwiftUI app and query it over a Unix socket. The app uses the Swift SDK to open seekdb and MySQLNIO to run a SQL query that combines full-text and vector search.
Prerequisites
- A Mac with Xcode 16 or later, an iOS 18 or later Simulator runtime, Git, and Homebrew.
- Swift SDK 1.4.1 for the seekdb V1.4.1 engine. Swift Package Manager downloads and verifies the XCFramework during dependency resolution. Xcode embeds the iOS device or Simulator slice in the app.
See the Swift SDK releases for release assets.
Set up the example project
Install XcodeGen and clone the Swift SDK at tag 1.4.1 to get the SeekDB Shell example.
SeekDB Shell source
brew install xcodegen
git clone --branch 1.4.1 --depth 1 https://github.com/oceanbase/seekdb-swift.git
cd seekdb-swift/example
Save the shared SQL example
as hybrid-search.sql in the current example directory. In project.yml,
update the sources list under targets.SeekDBShell to include the SQL file
as an app resource.
sources:
- App
- path: hybrid-search.sql
buildPhase: resources
Run the hybrid search
In App/SeekDBShellApp.swift, keep the license header and replace the
code from the import statements onward with the following code. Keep
App/QueryCommand.swift in the target. The example's QueryCommand reads MySQL
text results when the server uses EOF packets.
QueryCommand source
On the first run, use an unused data directory inside the app sandbox.
The code uses Documents/seekdb-hybrid-search, connects to the test
database, and sends one SQL statement at a time. The semicolon split is for
the fixed example file.
The app closes the SQL connection and shuts down the NIO event loop group
before it closes the seekdb handle. The engine stays active until the app
process exits. To repeat setup, stop the app and change
seekdb-hybrid-search in the code to a new directory name.
import Foundation
import MySQLNIO
import NIOCore
import NIOPosix
import SeekDB
import ShellCore
import SwiftUI
@main
struct SeekDBShellApp: App {
var body: some Scene {
WindowGroup {
HybridSearchView()
}
}
}
struct HybridSearchView: View {
@State private var output = "Running hybrid search..."
var body: some View {
ScrollView {
Text(output)
.font(.system(.body, design: .monospaced))
.textSelection(.enabled)
.padding()
}
.task {
output = await Task.detached {
do {
return try runHybridSearch()
} catch {
return "Hybrid search failed: \(error)"
}
}.value
print(output)
}
}
}
enum ExampleError: Error {
case missingSQLFile
case expectedFourStatements
case missingUnixSocket
case missingResults
}
func runHybridSearch() throws -> String {
guard let file = Bundle.main.url(forResource: "hybrid-search", withExtension: "sql") else {
throw ExampleError.missingSQLFile
}
let script = try String(contentsOf: file, encoding: .utf8)
let statements = script.split(separator: ";")
.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
guard statements.count == 4 else {
throw ExampleError.expectedFourStatements
}
let documents = try FileManager.default.url(
for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: true
)
let directory = documents.appendingPathComponent("seekdb-hybrid-search", isDirectory: true)
let db = try SeekDB.open(directory.path)
defer { try? db.close() }
let options = try db.connectionOptions()
guard options.transport == "unix_socket", let socket = options.unix_socket else {
throw ExampleError.missingUnixSocket
}
let group = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { try? group.syncShutdownGracefully() }
let connection = try MySQLConnection.connect(
to: try SocketAddress(unixDomainSocketPath: socket),
username: options.user ?? "root",
database: "test",
password: nil,
tlsConfiguration: nil,
on: group.next()
).wait()
defer { try? connection.close().wait() }
for statement in statements {
let command = QueryCommand(sql: statement)
try connection.send(command, logger: connection.logger).wait()
if case let .rows(columns, rows)? = command.results.first,
columns.count == 1, columns[0].name == "results",
rows.count == 1, let cell = rows[0].first, let result = cell {
return result
}
}
throw ExampleError.missingResults
}
Generate and open the Xcode project from the example directory.
xcodegen generate
open SeekDBShell.xcodeproj
In Xcode, choose the SeekDBShell scheme and an iOS Simulator. Select Product > Run.
The app displays the JSON from the results column and prints it to the Xcode
debug console. The expected order of IDs for the shared SQL example is
1, 3, and 2.
[
{
"id": 1,
"body": "SeekDB hybrid search",
"_score": 0.0325
},
{
"id": 3,
"body": "Combine keyword queries with vector retrieval",
"_score": 0.0164
},
{
"id": 2,
"body": "SeekDB installation guide for local application development",
"_score": 0.0161
}
]