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 如何使用Alamofire&;刷新Api授权令牌;rxSwift?_Ios_Swift_Alamofire_Rx Swift_Rxalamofire - Fatal编程技术网

Ios 如何使用Alamofire&;刷新Api授权令牌;rxSwift?

Ios 如何使用Alamofire&;刷新Api授权令牌;rxSwift?,ios,swift,alamofire,rx-swift,rxalamofire,Ios,Swift,Alamofire,Rx Swift,Rxalamofire,我尝试管理rxswift&Alamofire以获得响应。 这些函数在令牌未过期时成功获得响应。 但当令牌过期时,我不知道如何刷新令牌,然后使用新令牌重试获取响应。 我应该如何刷新令牌并重试? 我还阅读了Alamofire文档,找到了“RequestAdapter”和“RequestRetrier”。 我应该在我的案例中使用RequestAdapter和RequestRetrier吗? 但是我不知道如何在我的“getRequestJSON”函数中使用它们, 或者有什么好主意刷新令牌并重试。 谢谢。

我尝试管理rxswift&Alamofire以获得响应。
这些函数在令牌未过期时成功获得响应。
但当令牌过期时,我不知道如何刷新令牌,然后使用新令牌重试获取响应。
我应该如何刷新令牌并重试?
我还阅读了Alamofire文档,找到了“RequestAdapter”和“RequestRetrier”。
我应该在我的案例中使用RequestAdapter和RequestRetrier吗?
但是我不知道如何在我的“getRequestJSON”函数中使用它们,
或者有什么好主意刷新令牌并重试。
谢谢。

func get(_ callback: @escaping (JSON) -> Void) {
let url = "http://106.xx.xxx.xxx/user"
self.getRequestJSON( .get, url: url, params: [:], callback: { json in
    callback(json)
})
}

func getRequestJSON(_ method: Alamofire.HTTPMethod, url:String, params:[String:Any] = [:], callback: @escaping (JSON) -> Void) {

var headers:[String:String] = [String:String]()
if token.isEmpty == false {
    headers["Authorization"] = "Bearer \(token)"
}

let configuration = URLSessionConfiguration.default
configuration.httpAdditionalHeaders = SessionManager.defaultHTTPHeaders
configuration.timeoutIntervalForRequest = timeout

_ = SessionManager(configuration: configuration)
    .rx.responseJSON(method,
                     url,
                     parameters: params,
                     encoding: ((method == .get) ? URLEncoding.default : JSONEncoding.default),
                     headers: headers)
    .subscribeOn(SerialDispatchQueueScheduler.init(qos: .background))
    .subscribe(onNext: { (r, data) in

        if r.statusCode == 401 {

        //token fail 
        }            

        let json = JSON(data)

        if json["status"].stringValue == "successful" {

            callback(json)
        }else {
            callback(json)
        }
    }, onError: { (error) in

        callback(JSON(error))

    })
    .addDisposableTo(ResfulAPIDisposeBag)
}