This repository contains experimental Swift bindings for Loro CRDT.
If you have any suggestions for API, please feel free to create an issue or join our Discord community.
Uses Loro core and FFI 1.17.1. exportPendingUpdates() returns optional Data
for received updates waiting for missing dependencies. Save it with a consistent
snapshot; restore the snapshot first, then import the pending bytes. When it
returns nil, clear any older pending bytes as part of replacing the checkpoint.
undo() and redo() consume at most one history item per call. A remote edit
can make an item a no-op, returning false while canUndo() remains true.
The release build regenerates the Swift bindings and runs the Swift tests against
the new XCFramework before publishing its checksum and download URL.
release-artifact.json records the exact successful build and checksum used by
the release workflow, including retries.
Add the dependency in your Package.swift.
let package = Package(
name: "your-project",
products: [......],
dependencies:[
...,
.package(url: "https://github.com/loro-dev/loro-swift.git", from: "1.17.1")
],
targets:[
.executableTarget(
...,
dependencies:[.product(name: "Loro", package: "loro-swift")],
)
]
)import Loro
// create a Loro document
let doc = LoroDoc()
// create Root Container by getText, getList, getMap, getTree, getMovableList, getCounter
let text = doc.getText(id: "text")
try! text.insert(pos: 0, s: "abc")
try! text.delete(pos: 0, len: 1)
let s = text.toString()
// XCTAssertEqual(s, "bc")
// subscribe the event
let sub = doc.subscribeRoot{ diffEvent in
print(diffEvent)
}
// export updates or snapshot
let doc2 = LoroDoc()
let snapshot = doc.export(mode: ExportMode.snapshot)
let updates = doc.export(mode: ExportMode.updates(from: VersionVector()))
// import updates or snapshot
let status = try! doc2.import(snapshot)
let status2 = try! doc2.import(updates)
// import batch of updates or snapshot
try! doc2.importBatch(bytes: [snapshot, updates])
// checkout to any version
let startFrontiers = doc.oplogFrontiers()
try! doc.checkout(frontiers: startFrontiers)
doc.checkoutToLatest()If you wanna build and develop this project with MacOS, you need first run this script:
sh ./scripts/build_macos.sh
LOCAL_BUILD=1 swift testThe script will run uniffi and generate the loroFFI.xcframework.zip.
- uniffi-rs: a multi-language bindings generator for rust
- Automerge-swift:
loro-swiftuses many ofautomerge-swift's scripts for building and CI.