2

I'm trying to work with RxAlamofire to wrap a network request result.

My objective is to fire request, handle JSON response and create and Observable that contain either network operation success or any error occur.

In other place I can call the function that create the Observable and subscribe to it and notify user whether it is success or failure with error message.

My implementation is below:

func discoverMovieList(for url: String, withPagg page: Int) -> Observable<Any> {
        let requestUrl = "\(url)&page=\(page)"

        return RxAlamofire.json(.get, requestUrl)
            .map{ jsonResponse in
                    self.createOrUpdateMoviesList(from: JSON(jsonResponse))
                }
    }

How can we correct the code and how we call it from other place to notify the result of the process?

halfer
  • 19,824
  • 17
  • 99
  • 186
Lê Khánh Vinh
  • 2,591
  • 5
  • 31
  • 77
  • Can you explicit what the problem is with the current implementation? – tomahh Apr 06 '17 at 07:45
  • Hi thanks for your response. There no problem with the current implementation. I want to ask how can we use that to track the result of network request. for example execute other request only when the first has finished – Lê Khánh Vinh Apr 06 '17 at 07:48

1 Answers1

0

Observable define multiple lifecycle callback, that you provide using the subscribe method. In this instance, usage would be something like this:

discoverMovieList(for: "http://some.url", withPagg: 2)
  .subscribe(
    onNext: { movies in
      uiElement.string = "Movie list received"
    },
    onError: { error in
      uiElement.string = "Something went wrong"
    }
  )
  .disposed(by: disposeBag)

disposeBag is used to hold the subscription, so when disposeBag is released, the subscription is cancelled (in our case, the network call would be aborted).

tomahh
  • 13,441
  • 3
  • 49
  • 70
  • Thanks. my self.createOrUpdateMoviesList(from: JSON(jsonResponse)) only update coredata from jsonResponse (it return nothing) how can we chain the response. ie fire another request when update core data has completed? may be not using map at first place? – Lê Khánh Vinh Apr 06 '17 at 08:04