All Projects → safx → Observablearray Rxswift

safx / Observablearray Rxswift

Licence: mit
An array that can emit messages of elements and diffs on it's changing.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Observablearray Rxswift

Swiftrex
Swift + Redux + (Combine|RxSwift|ReactiveSwift) -> SwiftRex
Stars: ✭ 267 (+147.22%)
Mutual labels:  rxswift, observable
Async Ray
Provide async/await callbacks for every, find, findIndex, filter, forEach, map, reduce, reduceRight and some methods in Array.
Stars: ✭ 102 (-5.56%)
Mutual labels:  array
Orthogonalarraytest
OrthogonalArrayTest. 正交实验法设计测试用例,自动生成测试集。
Stars: ✭ 79 (-26.85%)
Mutual labels:  array
Nestedtypes
BackboneJS compatibility layer for Type-R data framework.
Stars: ✭ 94 (-12.96%)
Mutual labels:  observable
Rxkeyboard
Reactive Keyboard in iOS
Stars: ✭ 1,246 (+1053.7%)
Mutual labels:  rxswift
Rxstorekit
StoreKit library for RxSwift
Stars: ✭ 96 (-11.11%)
Mutual labels:  rxswift
Rxbluetoothkit
iOS & OSX Bluetooth library for RxSwift
Stars: ✭ 1,213 (+1023.15%)
Mutual labels:  rxswift
Rxviz
Rx Visualizer - Animated playground for Rx Observables
Stars: ✭ 1,471 (+1262.04%)
Mutual labels:  observable
Oba
Observe any object's any change
Stars: ✭ 101 (-6.48%)
Mutual labels:  observable
Qiitawithfluxsample
A sample project uses Flux and MVVM features with RxSwift.
Stars: ✭ 94 (-12.96%)
Mutual labels:  rxswift
Smart Array To Tree
Convert large amounts of data array to tree fastly
Stars: ✭ 91 (-15.74%)
Mutual labels:  array
Rxmarvel
Playing around marvel public API with RxSwift, Argo, Alamofire
Stars: ✭ 86 (-20.37%)
Mutual labels:  rxswift
Rerxswift
ReRxSwift: RxSwift bindings for ReSwift
Stars: ✭ 97 (-10.19%)
Mutual labels:  rxswift
Dyno
Package dyno is a utility to work with dynamic objects at ease.
Stars: ✭ 81 (-25%)
Mutual labels:  array
Rxearthquake
A sample app describing my philosophy on how to write iOS code with RxSwift.
Stars: ✭ 102 (-5.56%)
Mutual labels:  rxswift
Tiledb Py
Python interface to the TileDB storage manager
Stars: ✭ 78 (-27.78%)
Mutual labels:  array
Graphical Debugging
GraphicalDebugging extension for Visual Studio
Stars: ✭ 88 (-18.52%)
Mutual labels:  array
Proposal Array Unique
ECMAScript proposal for Deduplicating method of Array
Stars: ✭ 96 (-11.11%)
Mutual labels:  array
Rx React Container
Use RxJS in React components, via HOC or Hook
Stars: ✭ 105 (-2.78%)
Mutual labels:  observable
Boilerplate
Swift 4 and Using MVVM architecture(Rxswfit + Moya) to implement Github client demo.
Stars: ✭ 102 (-5.56%)
Mutual labels:  rxswift

TravisCI codecov.io Platform License Version Carthage

ObservableArray-RxSwift

ObservableArray is an array that can emit messages of elements and diffs when it is mutated.

Usage

ObservableArray has two Observables:

func rx_elements() -> Observable<[Element]>
func rx_events() -> Observable<ArrayChangeEvent>

rx_elements

rx_elements() emits own elements when mutated.

var array: ObservableArray<String> = ["foo", "bar", "buzz"]
array.rx_elements().subscribeNext { print($0) }

array.append("coffee")
array[2] = "milk"
array.removeAll()

This will print:

["foo", "bar", "buzz"]
["foo", "bar", "buzz", "coffee"]
["foo", "bar", "milk", "coffee"]
[]

Please note that rx_elements() emits the current items first when it has subscribed because it is implemented by using BehaviorSubject.

rx_elements can work with rx_itemsWithCellIdentifier:

model.rx_elements()
    .observeOn(MainScheduler.instance)
    .bindTo(tableView.rx_itemsWithCellIdentifier("MySampleCell")) { (row, element, cell) in
        guard let c = cell as? MySampleCell else { return }
        c.model = self.model[row]
        return
    }
    .addDisposableTo(disposeBag)

rx_events

rx_events() emits ArrayChangeEvent that contains indices of diffs when mutated.

var array: ObservableArray<String> = ["foo", "bar", "buzz"]
array.rx_events().subscribeNext { print($0) }

array.append("coffee")
array[2] = "milk"
array.removeAll()

This will print:

ArrayChangeEvent(insertedIndices: [3], deletedIndices: [], updatedIndices: [])
ArrayChangeEvent(insertedIndices: [], deletedIndices: [], updatedIndices: [2])
ArrayChangeEvent(insertedIndices: [], deletedIndices: [0, 1, 2, 3], updatedIndices: [])

ArrayChangeEvent is defined as:

struct ArrayChangeEvent {
    let insertedIndices: [Int]
    let deletedIndices: [Int]
    let updatedIndices: [Int]
}

These indices can be used with table view methods, such like insertRowsAtIndexPaths(_:withRowAnimation:). For example, the following code will enable cell animations when the source array is mutated.

extension UITableView {
    public func rx_autoUpdater(source: Observable<ArrayChangeEvent>) -> Disposable {
        return source
            .scan((0, nil)) { (a: (Int, ArrayChangeEvent!), ev) in
                return (a.0 + ev.insertedIndices.count - ev.deletedIndices.count, ev)
            }
            .observeOn(MainScheduler.instance)
            .subscribeNext { sourceCount, event in
                guard let event = event else { return }

                let tableCount = self.numberOfRowsInSection(0)
                guard tableCount + event.insertedIndices.count - event.deletedIndices.count == sourceCount else {
                    self.reloadData()
                    return
                }

                func toIndexSet(array: [Int]) -> [NSIndexPath] {
                    return array.map { NSIndexPath(forRow: $0, inSection: 0) }
                }

                self.beginUpdates()
                self.insertRowsAtIndexPaths(toIndexSet(event.insertedIndices), withRowAnimation: .Automatic)
                self.deleteRowsAtIndexPaths(toIndexSet(event.deletedIndices), withRowAnimation: .Automatic)
                self.reloadRowsAtIndexPaths(toIndexSet(event.updatedIndices), withRowAnimation: .Automatic)
                self.endUpdates()
            }
    }
}

You can use rx_autoUpdater(_:) with bindTo(_:) in your view contollers:

model.rx_events()
    .observeOn(MainScheduler.instance)
    .bindTo(tableView.rx_autoUpdater)
    .addDisposableTo(disposeBag)

Unfortunately, rx_autoUpdater doesn't work with rx_elements() binding to rx_itemsWithCellIdentifier(_:source:configureCell:cellType:), because it uses reloadData() internally.

Supported Methods

ObservableArray implements the following methods and properties, which work the same way as Array's equivalent methods. You can also use additional Array methods defined in protocol extensions, such as sort, reverse and enumerate.

init()
init(count:Int, repeatedValue: Element)
init<S : SequenceType where S.Generator.Element == Element>(_ s: S)
init(arrayLiteral elements: Element...)

var startIndex: Int
var endIndex: Int
var capacity: Int

func reserveCapacity(minimumCapacity: Int)
func append(newElement: Element)
func appendContentsOf<S : SequenceType where S.Generator.Element == Element>(newElements: S)
func appendContentsOf<C : CollectionType where C.Generator.Element == Element>(newElements: C)
func removeLast() -> Element
func insert(newElement: Element, atIndex i: Int)
func removeAtIndex(index: Int) -> Element
func removeAll(keepCapacity: Bool = false)
func insertContentsOf(newElements: [Element], atIndex i: Int)
func replaceRange<C : CollectionType where C.Generator.Element == Element>(subRange: Range<Int>, with newCollection: C)
func popLast() -> Element?

var description: String
var debugDescription: String

subscript(index: Int) -> Element
subscript(bounds: Range<Int>) -> ArraySlice<Element>

Install

CocoaPods

pod 'ObservableArray-RxSwift'

Important: Use the following import statement to import ObservableArray-RxSwift:

import ObservableArray_RxSwift

Carthage

github "safx/ObservableArray-RxSwift"

Manual Install

Just copy ObservableArray.swift into your project.

License

MIT

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].