0

I have a function which is supposed to return an Observable. When I call the function for the first time, everything goes fine and I receive the result I expected, but for the second time that I want to make the same request with different parameters the function automatically returns the same response as before and it does not fire the requests at all. There are multiple requests nested because each new depends on the results of the previous.

It looks something like this:

func request1() -> Observable<String> {
    return RxAlamofire
            .requestString(.get, url)
            .map { (response, html) -> Result<String> in
                .....
                return newUrl
            }
}

private func request2(credentials: Credentials) -> Observable<String> {
   return request1()
            .flatMapLatest { (newUrl)

                return RxAlamofire
                    .requestString(.get, newUrl)
                    .flatMapLatest { (response, _) -> Observable<String> in

                        let params = ["username": credentials.username, "password": credentials.password]

                        return RxAlamofire
                            .requestString(.post, (response.url?.absoluteString)!, parameters: params)
                            .flatMapLatest { (response2,str) -> Observable<String> in

                                ... some code formating ...

                                return RxAlamofire
                                    .requestString(.post, actionUrl!, parameters: anotherParameters)
                                    .map { return result }
                        }
                }
        }
    }

when I am calling it it looks something like this :

result = someObservable.flatMapLatest { return ScrapingService.request2(credentials: $0) }

when I trigger "someObservable" it runs the request2 as expected, but the actual request is not made.

In the end I do onNext on the result.

eja08
  • 4,600
  • 3
  • 13
  • 19

1 Answers1

0

when I am calling it it looks something like this :

result = someObservable.flatMapLatest { return ScrapingService.request2(credentials: $0) }

You're not "calling" the observable here. You are merely creating it. In order to get results from an Observable, you must subscribe to it.

dalton_c
  • 6,876
  • 1
  • 33
  • 42
  • I see, I thought that the flatMapLatest would trigger it and when I subscribe to the "result" the outcome would be the same. If I wanted to assign the result of request2 to some observable and then subscribed to the observable, would it be triggered? like this: observable = ScrapingService.request2(..) observable.subscribe(onNext: {}) I don't know if I get the concept right. – eja08 Mar 18 '19 at 18:18
  • Yes, that would work. By using `flatMapLatest`, you're declaring a transformation on an Observable, but you still need to subscribe. This is known as a "cold" Observable, i.e. one that does not start emitting until it has been subscribed to. You can read more about "Hot" vs "Cold" Observables here: http://reactivex.io/documentation/observable.html – dalton_c Mar 18 '19 at 19:06
  • Thank you, that helped me a lot! – eja08 Mar 19 '19 at 19:30