Loop to 0 value from API

Asked 2 years ago, Updated 2 years ago, 57 views

The API responds with a number, but
until the value reaches zero. I'd like to keep throwing requests to the same API in Alamofire.
I can't think of how to code it.

If you want to receive the results in the order in which you throw the communication results,
How can I implement the action?

When enclosed in while statements, depending on the speed of the response,
I think the order of receipt will be different, but what do you think?

override func viewWillAppear(animated:Bool){
    super.viewWillAppear(animated)

    /* I want to loop this process. */
    getMessageList(apiResponse:{response in)
        省略 omission
    })
    /*--------------------------------*/
}

funcgetMessageList(apiResponse:(responseData:Int)->()){

    Alamofire.request (.GET, "(API URL), headers:customHeader)
        .responseJSON {response in}
            guard let object = response.result.value else {
                return
            }
            let json=JSON(object)
            let number: Int=json ["result"]["number"].int!
            apiResponse(responseData:number)
    }

}

I have also posted to teratail.
It's not urgent, but I'd appreciate it if you could give me an answer as soon as possible.

Thank you for your cooperation.

swift swift2

2022-09-29 22:32

1 Answers

I received a reply via teratail.

private var_response_numbers:Array<Int>=[]
    override func viewWillAppear(animated:Bool) {
        super.viewWillAppear(animated)

        /* I want to loop this process. */
        getMessageList()
        /*--------------------------------*/

}

funcgetMessageList(){

    Alamofire.request (.GET, "(API URL), headers:customHeader)
        .responseJSON {response in}
            guard let object = response.result.value else {
                return
            }
            let json=JSON(object)
            let number: Int=json ["result"]["number"].int!
            _response_number.append(number)
            if number! = 0 {
              self.getMessageList()
            }
    }

}


2022-09-29 22:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.