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

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

我正在尝试使用 RxAlamofire 创建 return Observable<(HTTPURLResponse, Any)> 的函数:

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()
        }
    }    
}

但我收到以下警告:-

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

如何解决?添加 .disposed(by: disposeBag) 后,我的功能无法正常工作。

您好,您必须将您的请求添加到 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()
    }

应该可以。