如何将 RxSwift 转换为组合框架
Posted
技术标签:
【中文标题】如何将 RxSwift 转换为组合框架【英文标题】:How can ı convert RxSwift to Combine Framework 【发布时间】:2020-09-18 08:15:41 【问题描述】:我想将此函数转换为在组合框架中编写。我尝试了几种方法,但没有奏效。任何人都可以对此有所了解吗?
func requestAuth(serviceParams: AuthServiceParams) -> Observable<AuthResponse>
return Observable.createobserver -> Disposable in
// buradaki işlemler rx 'te yapılacak.
AlamofireService.auth(serviceParams: serviceParams).responseObject
(response:DataResponse<AuthResponse>) in
if(response.error != nil )
print("❌⭕️❌ Auth login hatalı bir dönüş aldı sorun var.")
observer.onError(response.error!)
observer.onCompleted()
return
else
if let data = response.result.value
guard let token = data.data?.token else
print("TOKEN BULUNAMADI")
let authResponse = AuthResponse(
result: "fault",
success: false,
data: nil,
message: "Kullanıcı adı veya şifre hatalı",
errCode: "E0000"
)
observer.onNext(authResponse)
return
print("AuthLogin Token -------> \(token)")
ApplicationVariables.token = token
ApplicationVariables.customer = data.data?.customer
ApplicationVariables.config = data.data?.store?.config
ApplicationVariables.logo = data.data?.store?.logo
observer.onNext(data)
else
let error = NSError(domain: "Bir sorun oluştu. Lütfen yöneticinize başvurunuz.", code: 1001, userInfo: nil)
observer.onError(error)
observer.onCompleted()
return Disposables.create()
【问题讨论】:
【参考方案1】:您能得到的最接近的方法是使用Future
,但请记住,期货是急切的。如果您希望函数仍然是惰性的,则必须将其包装在 defer
调用中。
我还清理了代码,以便以更线性的方式阅读。
func requestAuth(serviceParams: AuthServiceParams) -> Future<AuthResponse, Error>
Future promise in
AlamofireService.auth(serviceParams: serviceParams).responseObject response in
guard response.error == nil else
print("❌⭕️❌ Auth login hatalı bir dönüş aldı sorun var.")
promise(.failure(response.error!))
return
guard let data = response.result.value else
let error = NSError(domain: "Bir sorun oluştu. Lütfen yöneticinize başvurunuz.", code: 1001, userInfo: nil)
promise(.failure(error))
return
guard let token = data.data?.token else
print("TOKEN BULUNAMADI")
let authResponse = AuthResponse(
result: "fault",
success: false,
data: nil,
message: "Kullanıcı adı veya şifre hatalı",
errCode: "E0000"
)
promise(.success(authResponse))
return
print("AuthLogin Token -------> \(token)")
ApplicationVariables.token = token
ApplicationVariables.customer = data.data?.customer
ApplicationVariables.config = data.data?.store?.config
ApplicationVariables.logo = data.data?.store?.logo
promise(.success(data))
【讨论】:
以上是关于如何将 RxSwift 转换为组合框架的主要内容,如果未能解决你的问题,请参考以下文章
如何将sender()名称转换为int [duplicate]