简体   繁体   中英

Cannot assign value of type 'RequestRetrier?' to type 'RequestRetrier?'

I am trying to make the request retrier like the example in Alamofire page, but I am getting this error:

在此处输入图片说明

Here is Request Retrier code:

public typealias RequestRetryCompletion = (_ shouldRetry: Bool, _ timeDelay: TimeInterval) -> Void

public protocol RequestRetrier {
func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion)}

class RequestRetryHandler: RequestRetrier {
public func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
    if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
        completion(true, 10.0) // retry after 1 second

    } else {
        completion(false, 0.0) // don't retry
    }
}}

And this is where the error happens:

let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler() as? RequestRetrier

You shouldn't be redeclaring the protocol or the RequestRetryCompletion . That's already done for you in Alamofire. All you need to do is the following:

import Alamofire
import Foundation

class RequestRetryHandler: RequestRetrier {
    func should(_ manager: SessionManager, retry request: Request, with error: Error, completion: @escaping RequestRetryCompletion) {
        if let response = request.task?.response as? HTTPURLResponse, response.statusCode == 503 {
            completion(true, 10.0) // retry after 1 second

        } else {
            completion(false, 0.0) // don't retry
        }
    }
}

let manager = Alamofire.SessionManager.default
manager.retrier = RequestRetryHandler()

Best of luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM