This Ditto extension library is now obsolete. DittoSwift version 1.1.1 and higher now include very similar APIs for the publishers that originally lived in this project.
Now, you can use liveQueryPublisher()
directly:
import DittoSwift
ditto.store.collection("products")
.find("categoryName == '\(categoryName)'")
.liveQueryPublisher()
- To run the example project, clone the repo, and run
pod install
from the Example directory first. - The example app is a to do list application and will require a Ditto license token. This file is ignored from the source control, you'll need to add it in.
- iOS 13.0 or higher
DittoSwift
1.0.4 or higher- A Ditto license token
CombineDitto is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'CombineDitto'
or if you want to use it from this source:
pod 'CombineDitto' :git => 'https://github.com/getditto/CombineDitto.git', :branch => 'master'
- Import
DittoSwift
andCombineDitto
import DittoSwift
import CombineDitto
- Create a query an get a publisher:
@Published var snapshot: Snapshot = ditto.store["todos"].findAll().publisher()
- Or you can publish a single query:
@Published var snapshot: SingleSnapshot = ditto.store["todos"].findByID("123abc").publisher()
- Construct a Ditto instance. Usually this can be in the
AppDelegate.swift
code like so:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
static var ditto: Ditto!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
Self.ditto = Ditto()
Self.ditto.setAccessLicense("YOUR LICENSE TOKEN HERE")
Self.ditto.startSync()
return true
}
}
2. Create a data source:
class DataSource: ObservableObject {
@Published var documents = [DittoDocument]()
var cancellables = Set<AnyCancellable>()
private let todoPublisher = AppDelegate.ditto.store["todos"].findAll().publisher()
private let faker = Faker()
func start() {
todoPublisher
.map({ (documents, event) in
return documents
})
.assign(to: &$todos)
}
}
- In your content view's
onAppear
handler, callstart()
to start the liveQuery and bind to the data!
struct ContentView: View {
@ObservedObject var dataSource: DataSource
var body: some View {
NavigationView {
ForEach(dataSource.documents, id: \.id) { document in
Text(document["text"].stringValue)
}
}
}.onAppear(perform: {
dataSource.start()
})
}
CombineDitto is available under the MIT license. See the LICENSE file for more info.