SDK Setup Guides
...
Quick Tips for Swift
Testing
testing peer to peer systems can be challenging, but worth the initial effort to ensure your data model works across multiple instances it is important to test your business logic with end to end unit tests that involve multiple ditto peers a typical test lifecycle is create two instances of ditto with the same identity and different directories call startsync() on both instances listen for changes to data using subscribe() and write tests as a response to the observelocal() function clean up ditto directories between tests and stop any open synchronization threads using stopsync() for complete examples and running tests, see the https //github com/getditto/samples/tree/master/testing/ getting started https //legacydocs ditto live/ios/quick tips/testing#getting started to test ditto using swiftui, we provide you with some basic convenience functions you can use to get started for code that you can use in your project, https //github com/getditto/samples/tree/master/testing/swiftui create custom directories https //legacydocs ditto live/ios/quick tips/testing#create custom directories each instance of ditto should use a different directory you can create a convenience class to provide a custom directory for every instance of ditto /// test helper which wraps a ditto instance public class dittoinstance { internal var ditto ditto? private let instancedir url public init(appid string) { // no need for cleanup, as the testssetup class is configured as nsprincipalclass // and will delete topleveldittodir() before any test job is run let instancedir = topleveldittodir() appendingpathcomponent(appid) appendingpathcomponent(uuid() uuidstring) self instancedir = instancedir self ditto = ditto( identity onlineplayground(appid appid, persistencedirectory instancedir), persistencedirectory instancedir ) } public func stop() { self ditto! stopsync() self ditto = nil } } func topleveldittodir() > url { let filemanager = filemanager default return try! filemanager url( for documentdirectory, in userdomainmask, appropriatefor nil, create false ) appendingpathcomponent("ditto top level") } create your test https //legacydocs ditto live/ios/quick tips/testing#create your test initialize two ditto instances with different directories call startsync() on both instances import xctest import dittoswift final class swiftuitests xctestcase { var ditto1 dittoinstance! var ditto2 dittoinstance! override func setup() { self ditto1 = dittoinstance(appid "test app") self ditto2 = dittoinstance(appid "test app") try! self ditto1 ditto? startsync() try! self ditto2 ditto? startsync() } func testexample() throws { // your test here } } listen for data changes https //legacydocs ditto live/ios/quick tips/testing#listen for changes to data using observelocal() , we listen to changes to data we write tests as a response to the observelocal() function based on what we expect to happen the tests should reside within the callback so that they properly test the state of the database after synchronization is complete let initialresultexpectation = expectation(description "initial event received") let docid = try! ditto1 ditto! store collection("cars") upsert(\["make" "toyota", "color" "red"]) let subscription = ditto2 ditto! store collection("cars") findall() subscribe() let livequery = ditto2 ditto! store collection("cars") findbyid(docid) observelocal { doc, event in if (!event isinitial) { xctassertequal(doc! value\["make"] as! string, "toyota") initialresultexpectation fulfill() } } wait(for \[initialresultexpectation], timeout 2) clean up https //legacydocs ditto live/ios/quick tips/testing#clean up don't forget to clean up between tests this can also be implemented as part of a base test class func testexample() throws { let initialresultexpectation = expectation(description "initial event received") let docid = try! ditto1 ditto! store collection("cars") upsert(\["make" "toyota", "color" "red"]) let subscription = ditto2 ditto! store collection("cars") findall() subscribe() let livequery = ditto2 ditto! store collection("cars") findbyid(docid) observelocal { doc, event in if (!event isinitial) { xctassertequal(doc! value\["make"] as! string, "toyota") initialresultexpectation fulfill() } } wait(for \[initialresultexpectation], timeout 2) livequery stop() ditto1 stop() ditto2 stop() } full example https //legacydocs ditto live/ios/quick tips/testing#full example for the full example that you can use in your project, https //github com/getditto/samples/tree/master/testing/android/app/src/androidtest/java/live/ditto/dittotesting `