swiftnetwork-programmingrx-swiftreactiverxalamofire

RxAlamofire request is not fired for the second time i call the function


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.


Solution

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