'Swift continues to assume no internet connection?

I have the following function for posting requests. My idea was if the request fails due to no internet connection to wait 3 seconds and then try again, but swift assumes that I still have no internet connection when I regain internet connection. It's a very strange error because when I step through the code with breakpoints it works the way I intended.

func PostQueueItem(queue: Queue) -> String{
    var toreturn: String = ""
    let components = URLComponents(string: queue.url)!
    //print(String(data: json!, encoding: String.Encoding.utf8))
    let myurl = components.url!
    var request = URLRequest(url: myurl)
    request.httpBody = queue.json
    request.httpMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    let session = URLSession.shared
    let group = DispatchGroup()
    group.enter()
    DispatchQueue.global(qos: .background).async{
        let task = session.dataTask(with: request, completionHandler: {(data, response, error) -> Void in
            do{
                if (data != nil){
                    let returndata = try JSONDecoder().decode(String.self, from: data!)
                    toreturn = returndata.components(separatedBy: "-").first!
                    print(toreturn)
                    group.leave()
                } else {
                    sleep(3)
                    group.leave()
                }
            } catch{
                print("error")
                print("Error info: \(error)")
                group.leave()
            }
        })
        task.resume()
    }
    group.wait()
    //customer number gets returned
    if (toreturn == ""){
        return PostCustomerAccount(queue: queue)
    }
    return toreturn
}

my queue struct

struct Queue: Codable{
    var postedTime: Date
    var title: String
    var subtitle: String
    var json: Data?
    var url: String
    var completeTime: Date?
    var returnString: String

    mutating func post(){
        returnString = PostCustomerAccount(queue: self)
        completeTime = Date()
    }
}

My Error

NSLocalizedDescription=The Internet connection appears to be offline

Thanks for any help!!



Solution 1:[1]

I removed the sleep in the PostQueueItem function and updated my struct to have the sleep. Now it works fine, I guess I just needed to separate the code a little instead of recursively calling the function.

struct Queue: Codable{
    var postedTime: Date
    var title: String
    var subtitle: String
    var json: Data?
    var url: String
    var completeTime: Date?
    var returnString: String

    mutating func post(){
        returnString = PostCustomerAccount(queue: self)
        completeTime = Date()
    }
    mutating func initPost(){
        if (returnString != ""){
            return
        } else {
            post()
            sleep(8)
            initPost()
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 micahkimel