This repository contains an example implementation of a Last-Write-Wins Element Set (LWW-Element-Set) in Swift, integrated with a SwiftUI interface for basic simulations.
- LWW-Element-Set CRDT: Implemented in Swift
- SwiftUI Interface: For basic element addition and removal simulations for two users
- Timestamp-Based Conflict Resolution
- Element List View: Showing elements added by each user with timestamp
- Swift 5.0 or later
- Xcode 12.0 or later
-
Clone the Repository
git clone https://github.com/ronstorm/LWWElementSet.git
-
Open the Project in Xcode
-
Build and Run the project on a simulator or real device
-
To Add an Element to the
LWW-Element-Setfor a particular user:let element = ElementWithUser(element: "Example", user: "UserA") let timestamp = Timestamp(replicaID: "UserA", counter: 1) elementSet.add(element: element, timestamp: timestamp)
-
To Remove an Element:
elementSet.remove(element: element, timestamp: newerTimestamp)
- In the SwiftUI interface, you can start and stop timers for two users (
User AandUser B). Each timer simulates adding random elements to theLWW-Element-Set.
-
You can view the added elements in a SwiftUI List. Each element is tagged with the user who added it and the timestamp.
List(elements, id: \.self) { element in Text("\(element.element) (\(element.user))") }
-
The
LWW-Element-Setimplementation in this project handles conflict resolution based on timestamps, ensuring that the last-written element wins.if addSet[element]! > removeSet[element]! { // Element is considered to be in the set }
- This project is licensed under the MIT License.
- For contributions, please open an issue first to discuss what you would like to change.