swiftrx-swiftrxalamofire

Result of call to 'subscribe(onNext:onError:onCompleted:onDisposed:)' is unused


I am trying to create function which return Observable<(HTTPURLResponse, Any)> using RxAlamofire:

func getResponse(credentialData: Credentials, ulr: String)->Observable<(HTTPURLResponse, Any)>{
        let credentialData = "\(credentialData.username):\(credentialData.password)".data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
        let base64Credentials = credentialData.base64EncodedString()
        let headers = ("Authorization", "Basic \(base64Credentials)")
        let header = HTTPHeaders.init(dictionaryLiteral: headers)

        return Observable.create{ observer in
            requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
            .subscribe(onNext: { response in
                observer.onNext(response)
                } ,
        onError: { error in
            observer.onError(error)
            })
        return Disposables.create()
        }
    }    
}

but I get this below warning:-

Result of call to 'subscribe(onNext:onError:onCompleted:onDisposed:)' is unused

How to fix it? After adding .disposed(by: disposeBag) my function isn't working.


Solution

  • Hello you have to add your request into a DisposeBag :

    return Observable.create { observer in
            requestJSON(.get, ulr, parameters: nil, encoding: JSONEncoding.default, headers: header)
                .subscribe(onNext: { response in
                    observer.onNext(response)
                }, onError: { error in
                    observer.onError(error)
                }).disposed(by: disposeBag)
            return Disposables.create()
        }
    

    That should works.