Rxswift: subscribe onNext not calling

Created on 28 Nov 2016  路  4Comments  路  Source: ReactiveX/RxSwift

Hi I am new to RxSwift and I am using RxAlamofire in iPhone development. Below is my code sample that is not working. Can anyone help me?

Below is my api service class which is responsible to get data from service and will return result to my model class.

import Foundation
import Alamofire
import RxCocoa
import RxSwift
import RxAlamofire
import ObjectMapper
import AlamofireObjectMapper

class MovieVillaAPIService {
func searchMovie(for search: String) -> Observable<Movie> {

        let params: [String: AnyObject] = [
            "api_key": "MyApiKey" as AnyObject,
            "query": search as AnyObject
        ]                

        return Observable.create { observer in            
            Alamofire.request(ResourcePath.search.path, method: HTTPMethod.get, parameters: params).responseObject(completionHandler: {(response: DataResponse<Movie>) in
                let movieResponse = response.result.value
                if response.result.error != nil {
                    observer.onError(response.result.error!)
                }
                else {
                    observer.onNext(movieResponse!)
                    observer.onCompleted()
                }
            })            
            return Disposables.create()
        }.shareReplay(1)
    }  
}

Below is my Model which will user my APIService class and and will use result given by service and responsible to update UI

import Foundation
import RxSwift
import RxCocoa

final class MovieVillaViewModel {
    private let movieService: MovieVillaAPIService    
    init() {
        let movieService = MovieVillaAPIService()                
    }

func searchMovie(searchString: String) {
        self.movieService.searchMovie(for: searchString).subscribe(onNext: {result in
            print(result)
        }, onError: {error in
            print(error)
        }).dispose()
    }
}

Facing problem here: When I am calling searchMovie using movieService it is calling service method and all went well with service. But I am not getting onNext back although observer is telling all subscriber to listen onNext. Where I am going wrong?

For more information below is my UIViewController class, where I am calling searchMovie method of ViewModel class.

import UIKit
import RxSwift
import RxCocoa

class SearchMovieViewController: UIViewController {

    @IBOutlet weak var txtSearch: TextField!        
    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        txtSearch.becomeFirstResponder()                        
        self.viewModel = MovieVillaViewModel()                
    }

    @IBAction func txtSearch_PrimaryAction(_ sender: UITextField) {
        if !(sender.text?.isEmpty)! {
            self.viewModel.searchMovie(searchString: sender.text!)            
        }
    }
}

Most helpful comment

Hi @viral-etymon ,

You are calling dispose immediately:

func searchMovie(searchString: String) {
        self.movieService.searchMovie(for: searchString).subscribe(onNext: {result in
            print(result)
        }, onError: {error in
            print(error)
        }).dispose() // <<----
    }
}

All 4 comments

Hi @viral-etymon ,

You are calling dispose immediately:

func searchMovie(searchString: String) {
        self.movieService.searchMovie(for: searchString).subscribe(onNext: {result in
            print(result)
        }, onError: {error in
            print(error)
        }).dispose() // <<----
    }
}

Thank you :-) It worked

I am having a similar problem, my methods gets called once (at the beginning when it does the first subscribe) but after that even though the value of the Variable gets changed the onNext does not get called. Although I though I was disposing of it correctly:

This is the variable created and changed on the VM
var currentTime: Variable<Float> = Variable(0.0)
then it gets updated inside of a function
self.currentTime.value = time
I doubled checked and in here the value gets updated but the onNext is never called. This is the code in the View Controller:

mediaPlayerObject.currentTime.asObservable() .subscribe( onNext: { time in print("reactive current time: \(time)") if (self.mediaPlayerView.switchSlide(currentSeconds: Double(time))) { print("changing slide") self.mediaPlayerView.setNextSlide() } }, onCompleted: { print("finished") }) .addDisposableTo(disposeBag)

Any help would be appreciated! thanks :)

What is the correct way of disposing ??

Was this page helpful?
0 / 5 - 0 ratings