Skip to content

Commit

Permalink
Update CodeExt.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
WindowsMEMZ authored Jan 17, 2024
1 parent 0939bbf commit a62e649
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions DarockBili Watch App/Extension/CodeExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,41 +139,35 @@ public func hideDigitalTime(_ b: Bool) {
}

// MARK: Networking
public func autoRetryRequestApi(_ url: String, headers: HTTPHeaders?, maxReqCount: Int = 30, callback: @escaping (JSON, Bool) -> Void) {
public func autoRetryRequestApi(_ url: String, headers: HTTPHeaders?, maxReqCount: Int = 10, callback: @escaping (JSON, Bool) -> Void) {
DispatchQueue.global().async {
var counter = 0
repeatReq(url, headers: headers, counter: &counter, maxReqCount: maxReqCount) { respJson, isSuccess in
callback(respJson, isSuccess)
}
}

func repeatReq(_ url: String, headers: HTTPHeaders?, counter: UnsafeMutablePointer<Int>, maxReqCount: Int, callback: @escaping (JSON, Bool) -> Void) {
singalReq(url, headers: headers) { respJson, isSuccess in
if isSuccess {
callback(respJson, true)
} else if counter.pointee < maxReqCount {
if debug {
tipWithText("\(counter.pointee)")
var reqResults = [JSON]()
var forTotal = 0
for i in 1...maxReqCount {
singalReq(url, headers: headers) { respJson, isSuccess in
DarockKit.Network.shared.requestJSON(url, headers: headers) { respJson, _ in
reqResults.append(respJson)
forTotal++
}
counter.pointee++
repeatReq(url, headers: headers, counter: counter, maxReqCount: maxReqCount, callback: callback)
} else if respJson != JSON() {
counter.pointee = 0
callback(respJson, true)
} else {
counter.pointee = 0
callback(respJson, false)
}
}
}
func singalReq(_ url: String, headers: HTTPHeaders?, callback: @escaping (JSON, Bool) -> Void) {
DarockKit.Network.shared.requestJSON(url, headers: headers) { respJson, isSuccess in
if isSuccess {
if CheckBApiError(from: respJson) {
callback(respJson, true)
while forTotal < maxReqCount { }
var isCalledback = false
var anyValidJson: JSON?
for result in reqResults {
if result != JSON() {
if CheckBApiError(from: result) {
callback(result, true)
isCalledback = true
break
} else {
callback(respJson, false)
anyValidJson = result
}
}
}
if !isCalledback {
if let vj = anyValidJson {
callback(vj, true)
} else {
callback(JSON(), false)
}
Expand Down

0 comments on commit a62e649

Please sign in to comment.