An observables framework for Swift
Swift Objective-C
Latest commit 8da6c67 Feb 21, 2017 @russellbstephens russellbstephens committed on GitHub Merge pull request #55 from UrbanCompass/remove-event
Remove subscribing with event

README.md

🐌 snail CircleCI Carthage compatible codecov.io SwiftPM Compatible

SNAIL

Installation

Carthage

You can install Carthage with Homebrew using the following command:

brew update
brew install carthage

To integrate Snail into your Xcode project using Carthage, specify it in your Cartfile where "x.x.x" is the current release:

github "UrbanCompass/Snail" "x.x.x"

Swift Package Manager

To install using Swift Package Manager have your Swift package set up, and add Snail as a dependency to your Package.swift.

dependencies: [
    .Package(url: "https://github.com/UrbanCompass/Snail.git", majorVersion: 0)
]

Manually

Add all the files from Snail/Snail to your project

Creating Observables

let observable = Observable<thing>()

Subscribing to Observables

observable.subscribe(
    onNext: { thing in ... }, // do something with thing
    onError: { error in ... }, // do something with error
    onDone: { ... } //do something when it's done
)

Closures are optional too...

observable.subscribe(
    onNext: { thing in ... } // do something with thing
)
observable.subscribe(
    onError: { error in ... } // do something with error
)

Creating Observables Variables

let variable = Variable<whatever>(some initial value)
let optionalString = Variable<String?>(nil)
optionalString.asObservable().subscribe(
    onNext: { string in ... } // do something with value changes
)

optionalString.value = "something"
let int = Variable<Int>(12)
int.asObservable().subscribe(
    onNext: { int in ... } // do something with value changes
)

int.value = 42

Miscellaneous Observables

let just = Just(1) // always returns the initial value (1 in this case)

enum TestError: Error {
  case test
}
let failure = Fail(TestError.test) //always fail with error

let n = 5
let replay = Replay(n) // replays the last N events when a new observer subscribes

Subscribing to Control Events

let control = UIControl()
control.controlEvent(.touchUpInside).subscribe(
  onNext: { ... }  // do something with thing
)

let button = UIButton()
button.tap.subscribe(
  onNext: { ... }  // do something with thing
)

Queues

You can specify which queue an observables will be notified on by using .subscribe(queue: <desired queue>). If you don't specify, then the observable will be notified on the same queue that the observable published on.

There are 3 scenarios:

  1. You don't specify the queue. Your observer will be notified on the same thread as the observable published on.

  2. You specified main queue AND the observable published on the main queue. Your observer will be notified synchronously on the main queue.

  3. You specified a queue. Your observer will be notified async on the specified queue.

Examples

Subscribing on DispatchQueue.main

observable.subscribe(queue: .main,
    onNext: { thing in ... }
)

Weak self

To avoid retain cycles and/or crashes, always use [weak self] when self is needed by an observer

observable.subscribe(onNext: { [weak self] in
    // use self? as needed.
})