iOS-未触发Alamofire RequestRetrier

时间:2018-09-13 14:53:35

标签: ios swift alamofire

我正在尝试实施一种重试机制,我看到alamofire有一个。 我正在尝试实现一种简单的重试机制,该机制具有多次请求请求,但是出了点问题。

const folderCollection = {
  0: {
    id: 194,
    name: "Test Update"
  },
  1: {
    id: 206,
    name: "Testssss"
  },
  2: {
    id: 197,
    name: "Test11111"
  },
  3: {
    id: 197,
    name: "Test11111"
  }
}

function removingElements(collection, id) {
  const array = Object.values(collection);
  const index = array.findIndex(elm => elm.id === id);
  return array.reduce((newCollection, elm, currentIndex) => {
    if (index === -1 || currentIndex <= index) {
      newCollection[currentIndex] = elm;
    }
    return newCollection;
  }, {});
}

const result =  removingElements(folderCollection, 206);

console.log(result);

这是我正在尝试使用的类:

class OAuth2Handler: RequestAdapter, RequestRetrier {
  func adapt(_ urlRequest: URLRequest) throws -> URLRequest {
    return urlRequest
 }

var defaultRetryCount = 4
private  var requestsAndRetryCounts: [(Request, Int)] = []
private  var lock = NSLock()

private func index(request: Request) -> Int? {
    return requestsAndRetryCounts.index(where: { $0.0 === request })
}

func addRetryInfo(request: Request, retryCount: Int? = nil) {
    lock.lock() ; defer { lock.unlock() }
    guard index(request: request) == nil else { print("ERROR addRetryInfo called for already tracked request"); return }

    requestsAndRetryCounts.append((request, retryCount ?? defaultRetryCount))
}

func deleteRetryInfo(request: Request) {
    lock.lock() ; defer { lock.unlock() }
    guard let index = index(request: request) else { print("ERROR deleteRetryInfo called for not tracked request"); return }

    requestsAndRetryCounts.remove(at: index)
}

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

    lock.lock() ; defer { lock.unlock() }

    guard let index = index(request: request) else { completion(false, 0); return }
    let (request, retryCount) = requestsAndRetryCounts[index]

    if retryCount == 0 {
        completion(false, 0)
    } else {
        requestsAndRetryCounts[index] = (request, retryCount - 1)
        completion(true, 0.5)
    }
 }
}

如果还有其他建议,我想听听他们

谢谢

2 个答案:

答案 0 :(得分:0)

for (let dipVolume of res) {似乎非常错误。您应该将其设置为sessionManager.adapter = RequestAdapter.self as? RequestAdapter的实例。

答案 1 :(得分:0)

所以她的问题是将请求添加到重试中,所以首先我做到了:

let sessionManager = SessionManager()
var retrier = OAuth2Handler()

override init() {
    sessionManager.retrier = retrier
}

在通话中,我的操作如下:

func sendRequest(url: String,meth: HTTPMethod,parameters: [String: AnyObject]?, success: @escaping (String, Data) -> Void, failure: @escaping (Error) -> Void) {
    let request = sessionManager.request(url, method: meth, parameters: parameters, encoding: JSONEncoding.default)
    retrier.addRetryInfo(request: request)

    self.asyncSerialWorker.enqueueWork { (done) in
        self.sessionManager.request(url, method:meth).responseJSON { (responseObject) -> Void in
            if responseObject.result.isSuccess {
                print("Generic succsess")
                let value = responseObject.result.value
                let json = JSON(value!)
                guard let result = responseObject.data else {return}
                success(self.parser.parseMaiden(json: json), result)
            }

            if responseObject.result.isFailure {
                let error : Error = responseObject.result.error!

                print("login failed")
                failure(error)
            }
            done()
        }
    }
}

如您所见,我已经向重试添加了一个请求:

              retrier.addRetryInfo(request: request)

也许我应该成功删除(将检查并更新)