Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 链接依赖API调用_Ios_Swift - Fatal编程技术网

Ios 链接依赖API调用

Ios 链接依赖API调用,ios,swift,Ios,Swift,我正在尝试链接两个API请求。一个用于生成新令牌,另一个用于获取元素列表(其中一个需要有效令牌) 这些方法的调用如下所示: AuthService.shared().authenticate() { result in DispatchQueue.main.async { switch result { case .failure (let error): // error pr

我正在尝试链接两个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"))
            }
        }
    }
我想做的是尝试调用
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
    }
}
  • 混乱的方式-只需按照您声明的顺序将所有3个调用嵌套在一起:

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