{"id":13719166,"url":"https://github.com/safx/ObservableArray-RxSwift","last_synced_at":"2025-05-07T11:31:19.353Z","repository":{"id":62449502,"uuid":"48803629","full_name":"safx/ObservableArray-RxSwift","owner":"safx","description":"An array that can emit messages of elements and diffs on it's changing.","archived":true,"fork":false,"pushed_at":"2018-03-26T16:40:12.000Z","size":48,"stargazers_count":112,"open_issues_count":4,"forks_count":25,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-13T00:35:41.791Z","etag":null,"topics":["array","observable","rxswift","swift"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/safx.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-30T14:14:59.000Z","updated_at":"2024-11-02T18:00:10.000Z","dependencies_parsed_at":"2022-11-01T22:31:33.422Z","dependency_job_id":null,"html_url":"https://github.com/safx/ObservableArray-RxSwift","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safx%2FObservableArray-RxSwift","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safx%2FObservableArray-RxSwift/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safx%2FObservableArray-RxSwift/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safx%2FObservableArray-RxSwift/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/safx","download_url":"https://codeload.github.com/safx/ObservableArray-RxSwift/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224592467,"owners_count":17337099,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["array","observable","rxswift","swift"],"created_at":"2024-08-03T01:00:43.584Z","updated_at":"2024-11-14T08:31:24.900Z","avatar_url":"https://github.com/safx.png","language":"Swift","funding_links":[],"categories":["Libraries"],"sub_categories":[],"readme":"\n[![TravisCI](http://img.shields.io/travis/safx/ObservableArray-RxSwift.svg?style=flat)](https://travis-ci.org/safx/ObservableArray-RxSwift)\n[![codecov.io](https://codecov.io/github/safx/ObservableArray-RxSwift/coverage.svg?branch=master)](https://codecov.io/github/safx/ObservableArray-RxSwift?branch=master)\n![Platform](https://img.shields.io/cocoapods/p/ObservableArray-RxSwift.svg?style=flat)\n![License](https://img.shields.io/cocoapods/l/ObservableArray-RxSwift.svg?style=flat)\n![Version](https://img.shields.io/cocoapods/v/ObservableArray-RxSwift.svg?style=flat)\n![Carthage](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)\n\n# ObservableArray-RxSwift\n\n`ObservableArray` is an array that can emit messages of elements and diffs when it is mutated.\n\n## Usage\n\n`ObservableArray` has two `Observable`s:\n\n```swift\nfunc rx_elements() -\u003e Observable\u003c[Element]\u003e\nfunc rx_events() -\u003e Observable\u003cArrayChangeEvent\u003e\n```\n\n### `rx_elements`\n\n`rx_elements()` emits own elements when mutated.\n\n```swift\nvar array: ObservableArray\u003cString\u003e = [\"foo\", \"bar\", \"buzz\"]\narray.rx_elements().subscribeNext { print($0) }\n\narray.append(\"coffee\")\narray[2] = \"milk\"\narray.removeAll()\n```\n\nThis will print:\n\n    [\"foo\", \"bar\", \"buzz\"]\n    [\"foo\", \"bar\", \"buzz\", \"coffee\"]\n    [\"foo\", \"bar\", \"milk\", \"coffee\"]\n    []\n\nPlease note that `rx_elements()` emits the current items first when it has subscribed because it is implemented by using `BehaviorSubject`.\n\n`rx_elements` can work with `rx_itemsWithCellIdentifier`:\n\n```swift\nmodel.rx_elements()\n    .observeOn(MainScheduler.instance)\n    .bindTo(tableView.rx_itemsWithCellIdentifier(\"MySampleCell\")) { (row, element, cell) in\n        guard let c = cell as? MySampleCell else { return }\n        c.model = self.model[row]\n        return\n    }\n    .addDisposableTo(disposeBag)\n```\n\n### `rx_events`\n\n`rx_events()` emits `ArrayChangeEvent` that contains indices of diffs when mutated.\n\n```swift\nvar array: ObservableArray\u003cString\u003e = [\"foo\", \"bar\", \"buzz\"]\narray.rx_events().subscribeNext { print($0) }\n\narray.append(\"coffee\")\narray[2] = \"milk\"\narray.removeAll()\n```\n\nThis will print:\n\n    ArrayChangeEvent(insertedIndices: [3], deletedIndices: [], updatedIndices: [])\n    ArrayChangeEvent(insertedIndices: [], deletedIndices: [], updatedIndices: [2])\n    ArrayChangeEvent(insertedIndices: [], deletedIndices: [0, 1, 2, 3], updatedIndices: [])\n\n`ArrayChangeEvent` is defined as:\n\n```swift\nstruct ArrayChangeEvent {\n    let insertedIndices: [Int]\n    let deletedIndices: [Int]\n    let updatedIndices: [Int]\n}\n```\n\nThese indices can be used with table view methods, such like `insertRowsAtIndexPaths(_:withRowAnimation:)`.\nFor example, the following code will enable cell animations when the source array is mutated.\n\n```swift\nextension UITableView {\n    public func rx_autoUpdater(source: Observable\u003cArrayChangeEvent\u003e) -\u003e Disposable {\n        return source\n            .scan((0, nil)) { (a: (Int, ArrayChangeEvent!), ev) in\n                return (a.0 + ev.insertedIndices.count - ev.deletedIndices.count, ev)\n            }\n            .observeOn(MainScheduler.instance)\n            .subscribeNext { sourceCount, event in\n                guard let event = event else { return }\n\n                let tableCount = self.numberOfRowsInSection(0)\n                guard tableCount + event.insertedIndices.count - event.deletedIndices.count == sourceCount else {\n                    self.reloadData()\n                    return\n                }\n\n                func toIndexSet(array: [Int]) -\u003e [NSIndexPath] {\n                    return array.map { NSIndexPath(forRow: $0, inSection: 0) }\n                }\n\n                self.beginUpdates()\n                self.insertRowsAtIndexPaths(toIndexSet(event.insertedIndices), withRowAnimation: .Automatic)\n                self.deleteRowsAtIndexPaths(toIndexSet(event.deletedIndices), withRowAnimation: .Automatic)\n                self.reloadRowsAtIndexPaths(toIndexSet(event.updatedIndices), withRowAnimation: .Automatic)\n                self.endUpdates()\n            }\n    }\n}\n```\n\nYou can use `rx_autoUpdater(_:)` with `bindTo(_:)` in your view contollers:\n\n```swift\nmodel.rx_events()\n    .observeOn(MainScheduler.instance)\n    .bindTo(tableView.rx_autoUpdater)\n    .addDisposableTo(disposeBag)\n```\n\nUnfortunately, `rx_autoUpdater` doesn't work with `rx_elements()` binding to `rx_itemsWithCellIdentifier(_:source:configureCell:cellType:)`, because it uses `reloadData()` internally.\n\n## Supported Methods\n\n`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`.\n\n```swift\ninit()\ninit(count:Int, repeatedValue: Element)\ninit\u003cS : SequenceType where S.Generator.Element == Element\u003e(_ s: S)\ninit(arrayLiteral elements: Element...)\n\nvar startIndex: Int\nvar endIndex: Int\nvar capacity: Int\n\nfunc reserveCapacity(minimumCapacity: Int)\nfunc append(newElement: Element)\nfunc appendContentsOf\u003cS : SequenceType where S.Generator.Element == Element\u003e(newElements: S)\nfunc appendContentsOf\u003cC : CollectionType where C.Generator.Element == Element\u003e(newElements: C)\nfunc removeLast() -\u003e Element\nfunc insert(newElement: Element, atIndex i: Int)\nfunc removeAtIndex(index: Int) -\u003e Element\nfunc removeAll(keepCapacity: Bool = false)\nfunc insertContentsOf(newElements: [Element], atIndex i: Int)\nfunc replaceRange\u003cC : CollectionType where C.Generator.Element == Element\u003e(subRange: Range\u003cInt\u003e, with newCollection: C)\nfunc popLast() -\u003e Element?\n\nvar description: String\nvar debugDescription: String\n\nsubscript(index: Int) -\u003e Element\nsubscript(bounds: Range\u003cInt\u003e) -\u003e ArraySlice\u003cElement\u003e\n```\n\n## Install\n\n### CocoaPods\n\n    pod 'ObservableArray-RxSwift'\n\n**Important**: Use the following `import` statement to import `ObservableArray-RxSwift`:\n\n`import ObservableArray_RxSwift`\n\n### Carthage\n\n    github \"safx/ObservableArray-RxSwift\"\n\n### Manual Install\n\nJust copy `ObservableArray.swift` into your project.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafx%2FObservableArray-RxSwift","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsafx%2FObservableArray-RxSwift","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafx%2FObservableArray-RxSwift/lists"}