Skip to content

DittoSwift extension methods for Apple's Combine Framework

License

Notifications You must be signed in to change notification settings

getditto/CombineDitto

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CombineDitto

Ditto logo

Version License Platform Platform

🚚 Moved - Now in DittoSwift!

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()

Legacy documentation

Running the example app

  1. To run the example project, clone the repo, and run pod install from the Example directory first.
  2. 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.

Requirements

  • iOS 13.0 or higher
  • DittoSwift 1.0.4 or higher
  • A Ditto license token

Installation

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'

Quick Start

  1. Import DittoSwift and CombineDitto
import DittoSwift
import CombineDitto
  1. Create a query an get a publisher:
@Published var snapshot: Snapshot = ditto.store["todos"].findAll().publisher()
  1. Or you can publish a single query:
@Published var snapshot: SingleSnapshot = ditto.store["todos"].findByID("123abc").publisher()

Usage with SwiftUI

  1. 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)
    }
}
  1. In your content view's onAppear handler, call start() 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()
    })
}

Author

www.ditto.live

License

CombineDitto is available under the MIT license. See the LICENSE file for more info.

About

DittoSwift extension methods for Apple's Combine Framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published