3lvis/Networking

Problem with changes on UI Threath

juanm4 opened this issue · 3 comments

Hi, I'm trying to understand how response works. I want to concatenate two post calls and update UI between first call and second call. For more details I'm posting my code here:

func downloadFiter() {
        self.view.makeToastActivity(message: "Loading filter...")
        let url = "https://..."
        let networking = Networking(baseURL: url)
        networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
            self.view.hideToastActivity()
            switch result {
            case .success(let response):
                downloadOferts()
                break
            case .failure(let response):
                self.view.makeToast(message: response.error.description)
                break
            }
        }
    }

func downloadOferts() {
        self.view.makeToastActivity(message: "Loading oferts...")
        let url = "https://..."
        let networking = Networking(baseURL: url)
        networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
                self.view.hideToastActivity()
                switch result {
                case .success(let response):
                    break
                case .failure(let response):
                    self.view.makeToast(message: response.error.description)
                    break
                }
        }
    }

Well, the problem I have is next: I call to function downloadFilter() and toast activity is showed. When the call ends, toast is hidden and it call to function downloadOferts(), but the second toast (it is inside downloadOferts() function) doesn't show and I don't know why. I know that problems is related to Dispatch Central, but I don't know where.

Can someone tell me what's happening?

Thanks in advance!

3lvis commented

Hi @juanm4,

Correct, the problem is the asynchronicity of things. The short answer is maybe you can try something like this

self.view.makeToastActivity(message: "Loading filter...")
let url = "https://..."
let networking = Networking(baseURL: url)
networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
    self.view.hideToastActivity()
    switch result {
    case .success(let response):
        self.view.makeToastActivity(message: "Loading oferts...")
        networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
                self.view.hideToastActivity()
                switch result {
                case .success(let response):
                    break
                case .failure(let response):
                    self.view.makeToast(message: response.error.description)
                }
        }
    case .failure(let response):
        self.view.makeToast(message: response.error.description)
    }
}

Hi @3lvis, I have checked your solution but it doesn't work. The code has the same behavior.

So I have decided show only one Toast which is hidden only on the second call (I havent't found a way to show both messages). At the end this is mi code:


self.view.makeToastActivity(message: "Loading...")
let url = "https://..."
let networking = Networking(baseURL: url)
networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
    switch result {
    case .success(let response):
        networking.post("", parameterType: .formURLEncoded, parameters: []) { result in
                self.view.hideToastActivity()
                switch result {
                case .success(let response):
                    break
                case .failure(let response):
                    self.view.makeToast(message: response.error.description)
                }
        }
    case .failure(let response):
        self.view.hideToastActivity()
        self.view.makeToast(message: response.error.description)
    }
}

Thanks for your time and best regards!

3lvis commented

Any time, Juanma! Ping me if you need help with anything else :)