How do I test for flow / order of execution? - swift

I'm trying to add a feature to show loading screen to this code:
func connect(with code: String) {
interactor.connect(with: code)
.subscribe(onNext: { displaySuccessScreenRelay.accept(()) },
onError: { displayErrorScreenRelay.accept(()) } )
.disposed(by: disposeBag)
}
I've made a behavior relay called loadingScreenShownRelay ad I know the proper way to do this is like this:
func connect(with code: String) {
loadingScreenShownRelay.accept(true)
interactor.connect(with: code)
.subscribe(onNext: {
displaySuccessScreenRelay.accept(())
loadingScreenShownRelay.accept(false)
},
onError: {
displayErrorScreenRelay.accept(())
loadingScreenShownRelay.accept(false)
})
.disposed(by: disposeBag)
}
Now the question is how do I rearrange the test so that I can test not only the logic but the order of showLoading -> success -> hideLoading?
I can probably test the displays and loadingScreenShown observables separately into 2 tests (ie. one to test the logic of input-> emit display success / error and one more to test the loadingScreenShown). But how do I know that the order was indeed showLoading -> success -> hideLoading ? If I do the tests without regards to the order, I can also do this and the tests will still go green.
func connect(with code: String) {
loadingScreenShownRelay.accept(true)
loadingScreenShownRelay.accept(false)
interactor.connect(with: code)
.subscribe(onNext: { displaySuccessScreenRelay.accept(()) },
onError: { displayErrorScreenRelay.accept(()) } )
.disposed(by: disposeBag)
}
Thanks in advance.

Imperative code, such as you have, can be difficult to test. You will need to put the entire class under test and you will need a fake Interactor.
A test like this will show that your loading screen relay works:
class ExampleTests: XCTestCase {
func test() {
let scheduler = TestScheduler(initialClock: 0)
let sut = Example(interactor: FakeInteractor(connect: { _ in
scheduler.createColdObservable([.next(10, ()), .completed(10)]).asObservable()
}))
let result = scheduler.createObserver(Bool.self)
_ = sut.loadingScreenShownRelay
.bind(to: result)
sut.connect(with: "foo")
scheduler.start()
XCTAssertEqual(result.events, [.next(0, true), .next(10, false)])
}
}
You can see in the assert that the true event comes before the false event and they are separated by the time units it takes for the interactor to emit.
The above uses the RxTest library.

Related

ReactiveSwift pipeline flatMap body transform not executed

I have the following pipeline setup, and for some reason I can't understand, the second flatMap is skipped:
func letsDoThis() -> SignalProducer<(), MyError> {
let logError: (MyError) -> Void = { error in
print("Error: \(error); \((error as NSError).userInfo)")
}
return upload(uploads) // returns: SignalProducer<Signal<(), MyError>.Event, Never>
.collect() // SignalProducer<[Signal<(), MyError>.Event], Never>
.flatMap(.merge, { [uploadContext] values -> SignalProducer<[Signal<(), MyError>.Event], MyError> in
return context.saveSignal() // SignalProducer<(), NSError>
.map { values } // SignalProducer<[Signal<(), MyError>.Event], NSError>
.mapError { MyError.saveFailed(error: $0) } // SignalProducer<[Signal<(), MyError>.Event], MyError>
})
.flatMap(.merge, { values -> SignalProducer<(), MyError> in
if let error = values.first(where: { $0.error != nil })?.error {
return SignalProducer(error: error)
} else {
return SignalProducer(value: ())
}
})
.on(failed: logError)
}
See the transformations/signatures starting with the upload method.
When I say skipped I mean even if I add breakpoints or log statements, they are not executed.
Any idea how to debug this or how to fix?
Thanks.
EDIT: it is most likely has something to do with the map withing the first flatMap, but not sure how to fix it yet.
See this link.
EDIT 2: versions
- ReactiveCocoa (10.1.0):
- ReactiveObjC (3.1.1)
- ReactiveObjCBridge (6.0.0):
- ReactiveSwift (6.1.0)
EDIT 3: I found the problem which was due to my method saveSignal sending sendCompleted.
extension NSManagedObjectContext {
func saveSignal() -> SignalProducer<(), NSError> {
return SignalProducer { observer, disposable in
self.perform {
do {
try self.save()
observer.sendCompleted()
}
catch {
observer.send(error: error as NSError)
}
}
}
}
Sending completed make sense, so I can't change that. Any way to change the flatMap to still do what I intended to do?
I think the reason your second flatMap is never executed is that saveSignal never sends a value; it just finishes with a completed event or an error event. That means map will never be called, and no values will ever be passed to your second flatMap. You can fix it by doing something like this:
context.saveSignal()
.mapError { MyError.saveFailed(error: $0) }
.then(SignalProducer(value: values))
Instead of using map (which does nothing because there are no values to map), you just create a new producer that sends the values after saveSignal completes successfully.

Chaining observables (that are network requests)

I have a network request called login that returns an Observable<UserInfo>. I need to make another API call from that result based on whether the data returned from login has a count > 1, otherwise, I just need to go to a different view controller. I’m trying to use flatMapLatest to do the check for the first request login and make the next network call jobStates (which returns an Observable<JobState>, but I don’t think I’m arranging them correctly. Any ideas? Is there a better / easier way to do this?
Here's what it looks like:
I would expect to see something like this:
func login() {
let loginResult = networkService
.login(login: usernameTextField.text!, password: passwordTextField.text!)
.share()
loginResult
.filter { $0.count > 1 }
.subscribe(onNext: { userInfo in
// stop here and go to a different view with userInfo data
})
.disposed(by: disposeBag)
let networkService = self.networkService // so you don't have to capture self below
loginResult
.filter { $0.count <= 1 }
.flatMapLatest { networkService.jobStates(locationId: $0.locationId) }
.subscribe(
onNext: { data in
// do whatever with data from second request
},
onError: { error in
// if either request errors, you will end up here.
})
.disposed(by: disposeBag)
}
When you have two different possible outcomes, you need two different subscribes.

Chaining operations with RxSwift

I want to chain following operations
createUserandVerify
Create Anonymous User (user)
Verify User -> verifiedUser
If verification successful return verifiedUser else return user
Get stuff with coredata getStuff
If stuff.count > 0 Upload stuff with user credentials uploadStuff
Finally report the result of all operations
I wrote createUserandVerify as below. I wonder how should I write uploadStuff in reactive way. Upload function depends on user credentials. Therefore It must only run after createUserandVerify. I know I could just check count of array inside uploadStuff and return empty but I wonder the best practices.
func createUserandVerify() -> Single<User> {
return Service.sharedInstance.generateAnonUser()
.flatMap{ user in
if Service.sharedInstance.isOldRegisteredUser {
print("It is old user")
// We need to verify the receipt
return Service.sharedInstance.verifyReceipt()
.flatMap { verifiedUser in
print("Returning Verified new user [Verification Success]")
return Single.just((verifiedUser))
}.catchError{ error ->Single<User> in
print("Returning firstly created user [Verification Failed]")
print("Error Type: \(error)")
return Single.just(user)
}
} else {
//Normal anonymous old user
print("Returning firstly created user [Anonymous]")
return Single.just(user)
}
}
}
Assumptions (since I have not worked with Single I changed them to Observable):
func createUserandVerify() -> Observable<User>
func getStuff() -> [Stuff]
func uploadStuff(_ user: User) -> Observable<String>
createUserandVerify() should publish errors with onError so uploadStuff will not be called if something goes wrong.
Possible solution:
enum CustomError: Error {
case instanceMissing
case notEnoughStuff
}
createUserandVerify()
.flatMap { [weak self] (user) -> Observable<String> in
guard let strongSelf = self else { throw CustomError.instanceMissing }
guard strongSelf.getStuff().count > 0 else { throw CustomError.notEnoughStuff }
return strongSelf.uploadStuff(user)
}
.subscribe(
onNext: { stringResult in
// print result from 'uploadStuff'
print(stringResult)
},
onError: { error in
// will be reached if something goes
// wrong in 'createUserandVerify' or 'uploadStuff'
// or if one of your custom errors in 'flatMap' are thrown
print(error)
})
.disposed(by: disposeBag)
You could also make getStuff reactive by returning an Observable or Single and also include it in the chain via flatMap.

RxSwift Wait For Observable to Complete

I'm new to rxswift and here's my problem:
Suppose I have observable of actions: Observable.of("do1", "do2", "do3")
Now this observable mapped to function that returns observable:
let actions = Observable.of("do1", "do2", "do3")
func do(action: String) -> Observable<Result> {
// do something
// returns observable<Result>
}
let something = actions.map { action in return do(action) } ???
How can I wait for do1 to complete first, then execute do2, then do3?
Edit: Basically i want to achieve sequential execution of actions. do3 waits for do2 result, do2 waits for do1 result.
Edit2: I've tried using flatmap and subscribe, but all actions runs in parallel.
How can I wait for do1 to complete first, then execute do2, then do3?
I think concatMap solves the problem.
Lets say we have some service and some actions we need to perform on it, for instance a backend against with we'd like to authenticate and store some data. Actions are login and store. We can't store any data if we aren't logged in, so we need to wait login to be completed before processing any store action.
While flatMap, flatMapLatest and flatMapFirst execute observables in parallel, concatMap waits for your observables to complete before moving on.
import Foundation
import RxSwift
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let service: [String:Observable<String>] = [
"login": Observable.create({
observer in
observer.onNext("login begins")
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0, execute: {
observer.onNext("login completed")
observer.onCompleted()
})
return Disposables.create()
}),
"store": Observable.create({
observer in
observer.onNext("store begins")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.2, execute: {
observer.onNext("store completed")
observer.onCompleted()
})
return Disposables.create()
}),
]
// flatMap example
let observeWithFlatMap = Observable.of("login", "store")
.flatMap {
action in
service[action] ?? .empty()
}
// flatMapFirst example
let observeWithFlatMapFirst = Observable.of("login", "store")
.flatMapFirst {
action in
service[action] ?? .empty()
}
// flatMapLatest example
let observeWithFlatMapLatest = Observable.of("login", "store")
.flatMapLatest {
action in
service[action] ?? .empty()
}
// concatMap example
let observeWithConcatMap = Observable.of("login", "store")
.concatMap {
action in
service[action] ?? .empty()
}
// change assignment to try different solutions
//
// flatMap: login begins / store begins / store completed / login completed
// flatMapFirst: login begins / login completed
// flatMapLatest: login begins / store begins / store completed
// concatMap: login begins / login completed / store begins / store completed
let observable = observeWithConcatMap
observable.subscribe(onNext: {
print($0)
})
I just face the same problem, and finally found the solution.
I expect my devices will do disconnect followed by one another, so I did as follow:
I just create the func like
func disconnect(position: WearingPosition) -> Completable{
print("test run")
return Completable.create { observer in
print("test run 2")
// Async process{
// observer(.complete)
// }
return Disposables.create()
}
}
And use like:
self.disconnect(position: .left_wrist).andThen(Completable.deferred({
return self.disconnect(position: .right_wrist)
})).subscribe(onCompleted: {
// do some things
}) { (error) in
print(error)
}.disposed(by: self.disposeBag)
The key is the usage of " Completable.deferred "
I have tested with the "test run" printed
Use flatMap or flatMapLatest. You can find more about them at reactivex.io.
You can flatMap and subscribe to the Result observable sequence by calling subscribe(on:) on the final output.
actions
.flatMap { (action) -> Observable<Result> in
return self.doAction(for: action)
}
.subscribe(onNext: { (result) in
print(result)
})
func doAction(for action: String) -> Observable<Result> {
//...
}
Read:
https://medium.com/ios-os-x-development/learn-and-master-%EF%B8%8F-the-basics-of-rxswift-in-10-minutes-818ea6e0a05b

API call executed only one time using RxSwift

I have this code with seems to be correct. But it's only react on first search change. So the code is executed only one time. I tried to add concat(Observable.never()) to my getAl function but it still running only one time. Did I miss something ?
exists = search.asObservable()
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest { searchString -> Observable<Bool> in
guard !searchString.isEmpty else {
return Observable.empty()
}
return ServiceProvider.food.getAll(whereFoodName: searchString)
.flatMap({ (result) -> Observable<Bool> in
return Observable.just(result.count > 0)
})
}
Your code just return an Observable. To work with it you should observe it (or rather subscribe to it in Rx terminology)
You'll probably want something like this:
search.asObservable()
.throttle(0.3, scheduler: MainScheduler.instance)
.distinctUntilChanged()
.subscribe(onNext: { searchString in
let exists = ServiceProvider.food.getAll(whereFoodName: searchString).count > 0
print("Exists: \(exists)")
// Or do whatever you want with `exists` constant
// You could call a method to update UI
if exists {
self.button.enabled = true
}
})
.disposed(by: disposeBag) //disposeBag should be your property which will be deallocated on deinit