All Projects → roberthein → Observable

roberthein / Observable

Licence: mit
The easiest way to observe values in Swift.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Observable

Microfeatures Guidelines
📦📝 uFeatures guidelines
Stars: ✭ 315 (-8.96%)
Mutual labels:  libraries, cocoapods, carthage
Fscalendar
A fully customizable iOS calendar library, compatible with Objective-C and Swift
Stars: ✭ 9,829 (+2740.75%)
Mutual labels:  library, cocoapods, carthage
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-91.04%)
Mutual labels:  cocoapods, carthage, functional
Alertift
Swifty, modern UIAlertController wrapper.
Stars: ✭ 242 (-30.06%)
Mutual labels:  library, cocoapods, carthage
Rxswift
Reactive Programming in Swift
Stars: ✭ 21,163 (+6016.47%)
Mutual labels:  reactive, observer, functional
Redux Most
Most.js based middleware for Redux. Handle async actions with monadic streams & reactive programming.
Stars: ✭ 137 (-60.4%)
Mutual labels:  reactive, observable, functional
Loadingshimmer
An easy way to add a shimmering effect to any view with just one line of code. It is useful as an unobtrusive loading indicator.
Stars: ✭ 1,180 (+241.04%)
Mutual labels:  library, cocoapods, carthage
Arare
Lightweight curried functional programming library
Stars: ✭ 127 (-63.29%)
Mutual labels:  programming, library, functional
Lightweightobservable
📬 A lightweight implementation of an observable sequence that you can subscribe to.
Stars: ✭ 114 (-67.05%)
Mutual labels:  observable, observer, cocoapods
Rxdownloader
- Reactive Extension Library for Android to download files
Stars: ✭ 40 (-88.44%)
Mutual labels:  reactive, library, libraries
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-83.24%)
Mutual labels:  reactive, observer, observable
Ioniconskit
Use Ionicons in your Swift projects.
Stars: ✭ 310 (-10.4%)
Mutual labels:  cocoapods, carthage
Eachnavigationbar
A custom navigation bar for each view controller.
Stars: ✭ 314 (-9.25%)
Mutual labels:  cocoapods, carthage
Easyreveal
Android Easy Reveal Library
Stars: ✭ 338 (-2.31%)
Mutual labels:  library, easy
Xlactioncontroller
Fully customizable and extensible action sheet controller written in Swift
Stars: ✭ 3,228 (+832.95%)
Mutual labels:  cocoapods, carthage
Misterfusion
MisterFusion is Swift DSL for AutoLayout. It is the extremely clear, but concise syntax, in addition, can be used in both Swift and Objective-C. Support Safe Area and Size Class.
Stars: ✭ 314 (-9.25%)
Mutual labels:  cocoapods, carthage
Sclalertview
Beautiful animated Alert View. Written in Objective-C
Stars: ✭ 3,426 (+890.17%)
Mutual labels:  cocoapods, carthage
Event
The Hoa\Event library
Stars: ✭ 319 (-7.8%)
Mutual labels:  observer, library
Persei
Animated top menu for UITableView / UICollectionView / UIScrollView written in Swift
Stars: ✭ 3,395 (+881.21%)
Mutual labels:  cocoapods, carthage
Functionkit
A framework for functional types and operations designed to fit naturally into Swift.
Stars: ✭ 302 (-12.72%)
Mutual labels:  cocoapods, carthage

Observable

Observable is the easiest way to observe values in Swift.

How to

Create an Observable and MutableObservable

Using MutableObservable you can create and observe event. Using Observable you can observe event, in order to avoid side-effects on our internal API.

class SomeViewModel {
    /// Public property, that can be read / observed by external classes (e.g. view controller), but not changed.
    var position: Observable<CGPoint> = {
        return positionSubject
    }
    // Or use the helper method Observable.asObservable()
    // lazy var position = positionSubject.asObservable()

    /// Private property, that can be changed / observed inside this view model.
    private let positionSubject = MutableObservable(CGPoint.zero)
}

Create Observer with custom onDispose functionality

In some cases Observables require resources while they're active that must be cleaned up when they're disposed of. To handle such cases you can pass an optional block to the Observable initializer to be executed when the Observable is disposed of.

url.startAccessingSecurityScopedResource()
let observable = Observable([URL]()) {
    url.stopAccessingSecurityScopedResource()
}

Model Properties as @MutableObservable

Now mark your binded/mapped properties as observable and export public observable

//Private Observer
@MutableObservable var text: String = "Test"

//add observer

_text.observe { (newValue, oldValue) in
    print(newValue)
}.add(to: &disposable)
        
//Public Observer

var textObserve: ImmutableObservable<String> {
    return _text
}

Add an observer

position.observe { p in
    // handle new position
}

Add an observer and specify the DispatchQueue

position.observe(DispatchQueue.main) { p in
// handle new position
}

Change the value

position.wrappedValue = p

Stop observing new values

position.observe {
    // This will stop all observers added to `disposal`
    self.disposal.dispose()
}.add(to: &disposal)

Memory management

For a single observer you can store the returned Disposable to a variable

disposable = position.observe { p in

For multiple observers you can add the disposable to a Disposal variable

position.observe { }.add(to: &disposal)

And always weakify self when referencing self inside your observer

position.observe { [weak self] position in

Installation

CocoaPods

Observable is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'Observable'

Swift Package Manager

Observable is available through Swift Package Manager. Swift Package Manager (SwiftPM) is a tool for automating the distribution of Swift code. It is integrated into the swift compiler and from Xcode 11, SwiftPM got natively integrated with Xcode.

dependencies: [
    .package(url: "https://github.com/roberthein/Observable", from: "VERSION")
]

Migrations

1.x.y to 2.0.0

  • Observable is now MutableObservable
  • ImmutableObservable is now Observable
  • Observable.asImmutableObservable() is now Observable.asObservable()
  • Observable.value is now Observable.wrappedValue

Suggestions or feedback?

Feel free to create a pull request, open an issue or find me on Twitter.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].