gpt4 book ai didi

ios - 使用 MVVM swift 在 View DidLoad 上进行 Api 调用

转载 作者:行者123 更新时间:2023-11-28 07:20:14 25 4
gpt4 key购买 nike

我有一个应用程序,我想在 ViewController 中唤醒屏幕后进行 API 调用。基本上,我使用通用链接来激活 ViewCONtroller,当它显示 UIViewController 时,我想根据获得的数据进行 API 调用。我目前正在使用 MVVM 架构,我在下面添加了我的代码

我的 View 模型

    class EmailVerificationViewModel: ViewModel, ViewModelType {

struct Input {
let editEmailTrigger: Driver<Void>
}

struct Output {

}

let routeManager: BehaviorRelay<RouteMatchResult?>
let currentEmail: BehaviorRelay<String?>

init(routeManager: RouteMatchResult?, provider: Api, currentEmail: String?) {
self.routeManager = BehaviorRelay(value: routeManager)
self.currentEmail = BehaviorRelay(value: currentEmail)
super.init(provider: provider)
}

func transform(input: Input) -> Output {

// THE CALL I WANT TO MAKE
routeManager.errorOnNil().asObservable()

.flatMapLatest { (code) -> Observable<RxSwift.Event<User>> in
log("=========++++++++++++==========")
// guard let code = code else {return}
let params = code.values
let challengeId = Int(params["xxx"] as? String ?? "0")
let login = LoginResponseModel(identifier: params["xxxx"] as? String, key: params["xxxxxx"] as? String, oth: params["xxxxx"] as? String, id: 0, challengeId: challengeId)

return self.provider.postVerifyApp(challengeId: login.challengeId!, oth: login.oth!, identifier: login.identifier!)
.trackActivity(self.loading)
.trackError(self.error)
.materialize()
}.subscribe(onNext: { [weak self] (event) in
switch event {
case .next(let token):
log(token)
AuthManager.setToken(token: token)
// self?.tokenSaved.onNext(())
case .error(let error):
log(error.localizedDescription)
default: break
}
}).disposed(by: rx.disposeBag)

return Output()
}
}

我的 View Controller

override func bindViewModel() {
super.bindViewModel()
guard let viewModel = viewModel as? EmailVerificationViewModel else { return }

let input = EmailVerificationViewModel.Input(editEmailTrigger: editEmailBtn.rx.tap.asDriver())
let output = viewModel.transform(input: input)

viewModel.loading.asObservable().bind(to: isLoading).disposed(by: rx.disposeBag)
viewModel.parsedError.asObservable().bind(to: error).disposed(by: rx.disposeBag)

isLoading.asDriver().drive(onNext: { [weak self] (isLoading) in
isLoading ? self?.startAnimating() : self?.stopAnimating()
}).disposed(by: rx.disposeBag)


error.subscribe(onNext: { [weak self] (error) in
var title = ""
var description = ""
let image = R.image.icon_toast_warning()
switch error {
case .serverError(let response):
title = response.message ?? ""
}
self?.view.makeToast(description, title: title, image: image)
}).disposed(by: rx.disposeBag)
}

那么一旦应用程序捕捉到通用链接并加载,我如何才能像 THE CALL I WANT TO MAKE 那样对评论进行调用。基本上对 viewDidLoad 进行 API 调用

最佳答案

您示例中的代码远远超过回答问题所需的代码。以下是如何在 viewDidLoad 上进行网络调用:

class ViewController: UIViewController {

var viewModel: ViewModel!

private let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()

let input = ViewModel.Input()
let output = viewModel.transform(input: input)
output.viewData
.bind(onNext: { viewData in
// setup the view with viewData
})
.disposed(by: disposeBag)
}
}

class ViewModel {
struct Input { }
struct Output {
let viewData: Observable<ViewData>
}

init(api: API) {
self.api = api
}

func transform(input: Input) -> Output {
let viewData = api.networkCall()
.map { ViewData(from: $0) }
return Output(viewData: viewData)
}

let api: API
}

关于ios - 使用 MVVM swift 在 View DidLoad 上进行 Api 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58434322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com