automerge swift

Wrapper around Swift types that can be modified concurrently by different users, and merged again automatically (a CRDT).

102
7
Swift

Automerge-swift

The project is an Automerge implementation, a library of data structures for building collaboraative applications, in Swift.
Automerge is cross platform and cross-language, allowing you to provide collaboration support between browsers and native apps.

The API Documentation provides an overview of this library and how to use it.

Automerge Repo (Swift) is a supplemental library that extends this library.
It adds pluggable network and storage support for Apple platforms for a more “batteries included” result, and is tested with the JavaScript version of Automerge Repo.

The open-source iOS and macOS document-based SwiftUI App MeetingNotes provides a show-case for how to use Automerge to build a live, collaborative experience.
MeetingNotes builds over both this library and the repository to provide both WebSocket and peer to peer based networking in the app.

Quickstart

Add a dependency in Package.swift, as the following example shows:

let package = Package(
    ...
    dependencies: [
        ...
        .package(url: "https://github.com/automerge/automerge-swift.git", from: "0.5.2")
    ],
    targets: [
        .executableTarget(
            ...
            dependencies: [.product(name: "Automerge", package: "automerge-swift")],
            ...
        )
    ]
)

Now you can create a document and do all sorts of Automerge things with it

let doc = Document()
let list = try! doc.putObject(obj: ObjId.ROOT, key: "colours", ty: .List)
try! doc.insert(obj: list, index: 0, value: .String("blue"))
try! doc.insert(obj: list, index: 1, value: .String("red"))

let doc2 = doc.fork()
try! doc2.insert(obj: list, index: 0, value: .String("green"))

try! doc.delete(obj: list, index: 0)

try! doc.merge(other: doc2) // `doc` now contains {"colours": ["green", "red"]}

For more details on the API, see the Automerge-swift API documentation and the articles within.

Note: There was an earlier project that provided Swift language bindings for Automerge. The repository was renamed and archived, but is available if you are looking for it.