rx-swift

Chaining dependent observables

空扰寡人 提交于 2019-12-11 06:30:45
问题 I need to create dependent API calls where the second one needs a value returned by the first one. First thing that comes to mind is using flatMap ApiManager.shared .createReport(report: report) .flatMap { (report) -> Observable<Report> in return ApiManager.shared.createReportStep(reportID: report.ID) } createReport returns Observable<Report> where after successfull call returns updated Report model(with ID), after that I need to call API to create report step, where report.ID is needed.

Swift Combine: How can I convert `AnyPublisher<[Foo], *>` to `AnyPublisher<Foo, *>`?

好久不见. 提交于 2019-12-11 05:59:12
问题 How can I convert a publisher of array a certain element, to just a publisher of said element (but with more events)? e.g. how can I convert AnyPublisher<[Int], Never> to AnyPublisher<Int, Never> ? I think maybe what RxSwift offers with its from operator is similar to what I want to do. I guess I want the inverse of Combine collect? 回答1: Here is the code: func example(publisher: AnyPublisher<[Foo], Never>) -> AnyPublisher<Foo, Never> { return publisher .map { $0.publisher } .switchToLatest()

Subscribe to UINavigationController back button with RxSwift / RxCocoa

你说的曾经没有我的故事 提交于 2019-12-11 04:26:54
问题 I have a simple test project with a UINavigationController as my rootViewController . I push the first ViewController which has a + button as right bar button item, I subscribe to its taps to present a new ViewController (which is identical to the previous one). The push segue works as I expect. UIKit manages the back button for me and I think it is UINavigationController that makes the magic behind the scene. Now, what I need to do is subscribe to the back button, but I can't find how to do

RxTest: Undefined symbols for architecture x86_64 and arm64

ぐ巨炮叔叔 提交于 2019-12-11 02:45:40
问题 I am trying to run the following test from Chapter 16: Testing with RxTest of Raywenderlich RxSwift book: import XCTest import RxSwift import RxTest @testable import Testing class TestingViewModel : XCTestCase { var viewModel: ViewModel! var scheduler: ConcurrentDispatchQueueScheduler! override func setUp() { super.setUp() viewModel = ViewModel() scheduler = ConcurrentDispatchQueueScheduler(qos: .default) } func testColorNameIsRayWenderlichGreenWhenHexStringIs006636() { // 1 let

iOS RxSwift how to connect Core bluetooth to Rx sequences?

依然范特西╮ 提交于 2019-12-11 01:23:00
问题 I'm trying to create an observable sequence to indicate the status of Bluetooth on device. I'm using ReplaySubject<CBManagerState> , but am curious if there is something better, as I hear bad things about using onNext() What is the appropriate way to connect callback delegates to the RxSwift observable domain? class BluetoothStatusMonitor: NSObject, CBPeripheralManagerDelegate { let bluetoothStatusSequence = ReplaySubject<CBManagerState>.create(bufferSize: 1) var bluetoothPeripheralManager:

RxSwift: onDisposed activated before Alamofire return data

♀尐吖头ヾ 提交于 2019-12-10 18:18:34
问题 I'm trying to get the JSON data from REST (swift 2.3) with rxSwift and Alamofire. This is my code: func getArticles(articlesReq: ArticlesReq) - > Observable < [Article] > { return Observable < [Article] > .create { observer in let request = Alamofire.request(.POST, apiPath, parameters: DataHelper().convertStringToDictionary(JSONString), encoding: .JSON) .responseArray { (response: Response < [Article], NSError > ) in if let articlesArray = response.result.value { observer.on(.Next

rxswift error handle issue

核能气质少年 提交于 2019-12-10 18:12:52
问题 I have a BehaviorSubject named createObservable in my view model. And my view controller subscribe it. viewModel!.createObservable.subscribe(onNext: {[unowned self] (obj:PassbookModelType?) -> Void in if let _ = obj{ self.dismissVC() } }, onError: { (error) -> Void in print(error) }).addDisposableTo(self.dispose) I have a function named saveObject() also in the view model. If I click the navigation bar right item it will be emitted. And there is an error will send to createObservable 's

How can I apply a grace time using RX?

独自空忆成欢 提交于 2019-12-10 17:27:38
问题 I have an Observable<Bool> that emits true when an operation begins and false when it ends. I'd like to show a message while the operation is in progress, but only if it takes longer than two seconds to begin. Is there a way I can create an observable that I can bind my message to? Any help much appreciated! 回答1: If you switchMap (a flatMap where when a second item is emitted from the source the subscription to the original observable is unsubscribed and the subscription moves to the next)

Manually disposing a DisposeBag in RxSwift

守給你的承諾、 提交于 2019-12-10 12:53:23
问题 I want to cancel a request and one of the ways is to manually remove the disposable bag. .addDisposableTo(disposeBag) As I have the disposeBag object, is there a good way to cancel the request other than that I mentioned above? 回答1: You have to just change reference to your disposeBag object. Make it nil or assign new object to disposeBag. All request will be cancelled. 回答2: Another option, besides deallocating a DisposeBag , is to use CompositeDisposable , which has an explicit dispose

Rxswift What difference between Observable.of and Observable<String>.create

删除回忆录丶 提交于 2019-12-10 10:36:15
问题 I'm a newbie in rxSwift. I found some difference when create Observable by Observable.of and Observable<String>.create Observable<String>.create { observer in observer.onNext("1") observer.onNext("2") return Disposables.create() }.take(3).toArray().subscribe(onNext: { (item) in print(item) }).disposed(by: disposeBag) Not emitted util received at least 3 event. In this way observer never emitted. But with: Observable.of("1", "2") // 2 .take(3).toArray() .subscribe(onNext: { print($0) })