Ios 链接依赖API调用
我正在尝试链接两个API请求。一个用于生成新令牌,另一个用于获取元素列表(其中一个需要有效令牌) 这些方法的调用如下所示:Ios 链接依赖API调用,ios,swift,Ios,Swift,我正在尝试链接两个API请求。一个用于生成新令牌,另一个用于获取元素列表(其中一个需要有效令牌) 这些方法的调用如下所示: AuthService.shared().authenticate() { result in DispatchQueue.main.async { switch result { case .failure (let error): // error pr
AuthService.shared().authenticate() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error generating token", comment: "Signup"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("Token collected \(result)", comment: "Signup"))
}
}
}
ListService.shared().listAll() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error fetching list", comment: "List"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("List fetched \(result)", comment: "List"))
}
}
}
我想做的是尝试调用ListService.shared().listAll()
,如果调用失败并出现错误,我想调用AuthService.shared().authenticate()
,然后再次调用ListService.shared().listAll()
你认为这是一个什么样的好方法呢?你可能会觉得值得研究PromiseKit,如果不是现在,那么最终()。然后,您可以使用它执行以下操作:
/**
This is just a facer for the real list all.
It will do the list all request, doing the authentication if it needs too.
*/
func listAll() {
firstly {
// Do the real list all
}.recover {
// List all failed, so we will attempt to recover
firstly {
// Do the authenticate
}.then {
// Authenticate succeeded. Do the real list all
}
}.done {
// List all succeeded
}.catch {
// List all failed, and we tried to recover,
// but a part of that also failed
}
}
ListService.shared().listAll() { result in
switch result {
case .failure (let error):
// error processing
DispatchQueue.main.async {
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error fetching list", comment: "List"))
}
AuthService.shared().authenticate() { result in
switch result {
case .failure (let error):
DispatchQueue.main.async {
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error generating token", comment: "Signup"))
}
case .success (let result):
DispatchQueue.main.async {
self.alertError(errorTitle: "", errorText: NSLocalizedString("Token collected \(result)", comment: "Signup"))
}
ListService.shared().listAll() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error fetching list", comment: "List"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("List fetched \(result)", comment: "List"))
}
}
}
}
}
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("List fetched \(result)", comment: "List"))
}
}
listAll(
)方法第一次失败的最坏情况要好,导致总共3个API调用(listAll()
,authenticate())
,listAll()
authenticate()
方法以在本地存储令牌,并在点击API之前检查令牌是否存在。然后更新您的listAll()
方法,使其始终首先调用authenticate()
。这假设您的令牌可以用于多个API调用,不会在调用之间过期,等等:AuthService.shared().authenticate() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error generating token", comment: "Signup"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("Token collected \(result)", comment: "Signup"))
ListService.shared().listAll() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error fetching list", comment: "List"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("List fetched \(result)", comment: "List"))
}
}
}
}
}
}
class AuthService {
private var token: String?
static func shared() -> AuthService {
return AuthService()
}
func authenticate(completion: @escaping (Result<String, Error>) -> Void) {
if let token = token {
completion(.success(token))
return
}
// Make your API call here and store the token when it completes
}
}
class ListService {
static func shared() -> ListService {
return ListService()
}
func listAll(completion: @escaping (Result<Any, Error>) -> Void) {
AuthService.shared().authenticate() { authResult in
switch authResult {
case .failure (let error):
completion(.failure(error))
case .success (let token):
// Make your list API call here
}
}
}
}
ListService.shared().listAll() { result in
DispatchQueue.main.async {
switch result {
case .failure (let error):
// error processing
self.alertError(errorTitle: "", errorText: NSLocalizedString("Error fetching list", comment: "List"))
case .success (let result):
self.alertError(errorTitle: "", errorText: NSLocalizedString("List fetched \(result)", comment: "List"))
}
}
}